ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

C# Tuple and its use

.Net C# about 6 years ago || 11/15/2019 || 3.9 K View

  • Hide

C# tuple is a data structure Which contains a sequence of elements of different data types. Tuples were available before C# 7.0, but they were unproductive and had no language support. C# 7.0 introduces better tuples feature that enables semantic names for the fields of a tuple using new, more efficient tuple types. C# 7 introduced ValueTuple to reduce the limitations of Tuple and it is more easier to work with Tuple. 

Syntax

Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>

an example below creates a tuple with four elements:

Tuple<int, string, string, string > Customer = new Tuple <int, string, string, string >(1, "Shyam", "Nepal","Kathmandu");

We can use Tuples in the following Cases:

  1. If we want to return multiple values, from one method without using out or ref parameters.
  2. If we want to pass multiple values to one method through a single parameter.
  3. When we want to hold a database record or some values temporarily without creating a separate class.

Implementation:

using System;
namespace CSharpTupule
{
    class Program
    {
        static void Main(string[] args)
        {
            var Result = Calculate(12, 8);
            Console.WriteLine("Sum: " + Result.Item1);
            Console.WriteLine("Multiply: " + Result.Item2);
            Console.WriteLine("Subtraction: " + Result.Item3);
            Console.WriteLine("Divide: " + Result.Item4);
            Console.ReadLine();
        }
        public static Tuple<int, int, int, int> Calculate(int x, int y)
        {
            int Add = x + y;
            int Mul = x * y;
            int Sub = x - y;
            int Div = x / y;
            Tuple<int, int, int, int> t = Tuple.Create(Add, Mul, Sub, Div);
            return t;
        }
    }
}

Output:

 

  • 1
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Anthony M. Healey

Anthony M. Healey

Touch of Creativity and a bit of active on brain power

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey