System.ValueTuple – Extended C# Tuples

The C# version #7, the namespace System.Collections.Generics provided the unique collection type called Tuples. In tuples you are allowed to store multiple values belonging to different data types or objects. In my previous article, you can get overview on C# Tuple collection type that provides the basics of Tuples in C#. In this article you will learn extended features of Tuples collection type by importing System.ValueTuple.


System.ValueTuple – Advanced:

The NuGet repository has an extended package System.ValueTuple which implement the underlying types for tuples in C# and Visual Basic. You can reference System.ValueTuple from NuGet manager.

Once the System.ValueTuple is successfully referenced to your project now you can write functions that return tuple values. Prior to writing the code make sure that you have added a reference declaration at the beginning of your code file.

//Import System.ValueTuple reference
using System.CalueTuple;
public class Program
{
    public static void Main()
    {
	
    }
}

Examples:

In the next section you will see a few self explanatory example programs that explain how System.ValueTuple allows methods to return multiple return values.

Example 1:

In this example, the method GetStudentInfo() returns the strongly typed tuple (string, int) and assigned to the implicit variable info declared using var keyword. The student details accessed via info.Item1 and info.Item2 default tuple members.

using System.ValueTuple;
public class Program
{
    public static void Main()
    {
        var info = GetStudentInfo();
        Console.WriteLine(String.Format("Student name is {0} and age is {1}", info.Item1, info.Item2.ToString());
    }

    public static (string, int) GetStudentInfo()
    {
        return ("Jhon", 20);
    }
}

Example 2:

In this example, the method GetStudentInfo() returns the strongly typed tuple with member names (string, int) and assigned to the implicit variable info which is declared using var keyword. The student details accessed via info.StudentName and info.StudentAge explicit tuple members.

using System.ValueTuple;
public class Program
{
    public static void Main()
    {
        var info = GetStudentInfo();
        Console.WriteLine(String.Format("Student name is {0} and age is {1}", info.StudentName, info.StudentAge.ToString());
    }

    public static (string StudentName, int StudentAge) GetStudentInfo()
    {
        return ("Jhon", 20);
    }
}

Example 3:

In this example, the method GetStudentInfo() returns the strongly typed tuple (string, int) and assigned to the explicit variable info which is declared using (string, int). The student details accessed via info.Item1 and info.Item2 default tuple members.

using System.ValueTuple;
public class Program
{
    public static void Main()
    {
        (string, int) info = GetStudentInfo();
        Console.WriteLine(String.Format("Student name is {0} and age is {1}", info.Item1, info.Item2.ToString());
    }

    public static (string, int) GetStudentInfo()
    {
        return ("Jhon", 20);
    }
}

Example 4:

In this example, the method GetStudentInfo() returns the strongly typed tuple (string, int) and assigned to the explicit variable info which is declared using (string StudentName, int StudentAge) with tuple members. The student details accessed via info.StudentName and info.StudentAge explicit tuple members.

using System.ValueTuple;
public class Program
{
    public static void Main()
    {
        (string StudentName, int StudentAge) = GetStudentInfo();
        Console.WriteLine(String.Format("Student name is {0} and age is {1}", StudentName, StudentAge.ToString());
    }

    public static (string, int) GetStudentInfo()
    {
        return ("Jhon", 20);
    }
}

Example 5:

In the next example, you can see how tuples can be passed as method parameters.

using System.ValueTuple;
public class Program
{
    public static void Main()
    {
        (string StudentName, int StudentAge) Student1= ("Jhon", 18);
        (string StudentName, int StudentAge) Student2= ("Jim", 19);
        AddToStudentRecord1(Student1);
        AddToStudentRecord1(Student2);
    }

    public static void AddToStudentRecord1((string, int) student1)
    {
        Console.WriteLine(string.Format("Student {0} with age {1} is inserted into the record", student1.Item1, student1.Item2.ToStringin()));
    }

    public static void AddToStudentRecord2((string SName, int SAge) student2)
    {
        Console.WriteLine(string.Format("Student {0} with age {1} is inserted into the record", student2.SName, student2.SAge.ToString()));
    }
}

– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo