Friday, June 19, 2026

C# Example of ValueTuple as method parameter

ValueTuple allows grouping multiple values together without creating a separate class.

Example of ValueTuple as Method Parameter

You can pass multiple related values into a method as a single argument.

class Demo
{
    static void ShowPerson((string Name, int Age) person)
    {
        Console.WriteLine($"Name: {person.Name}");
        Console.WriteLine($"Age : {person.Age}");
    }
    static void Main()
    {
        (string Name, int Age) data = ("Ajeet", 25);
        ShowPerson(data);
    }
}

Output

Name: Ajeet

Age : 25

Note: You can also pass directly: ShowPerson(("Ajeet", 25));

No comments:

Post a Comment

Hot Topics