Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to create a ValueType with names
ValueTuples in C# allow you to create lightweight data structures with named fields. Introduced in C# 7, ValueTuples provide a convenient way to group multiple values together without creating a separate class or struct.
Note − For .NET Framework projects, you may need to add the System.ValueTuple NuGet package to use ValueTuples.
Installing System.ValueTuple Package
To add the System.ValueTuple package to your project −
- Go to your project
- Right click on the project in the Solution Explorer
- Select "Manage NuGet Packages"
- Click the Browse tab and search for "System.ValueTuple"
- Install the System.ValueTuple package
Syntax
Following is the syntax for creating a named ValueTuple −
var tupleName = (field1: value1, field2: value2, field3: value3);
You can also explicitly specify the type −
(int marks, string name, string subject) myTuple = (95, "Jack", "Maths");
Using Named ValueTuples
Example
using System;
class Program {
static void Main() {
var myTuple = (marks: 95, name: "Jack", subject: "Maths");
// Using names to access tuple elements
Console.WriteLine("Student Marks: " + myTuple.marks);
Console.WriteLine("Student Name: " + myTuple.name);
Console.WriteLine("Student Subject: " + myTuple.subject);
}
}
The output of the above code is −
Student Marks: 95 Student Name: Jack Student Subject: Maths
Multiple ValueTuples with Different Data
Example
using System;
class Program {
static void Main() {
var student1 = (id: 101, name: "Alice", grade: 'A', gpa: 3.8);
var student2 = (id: 102, name: "Bob", grade: 'B', gpa: 3.2);
Console.WriteLine($"Student 1: ID={student1.id}, Name={student1.name}, Grade={student1.grade}, GPA={student1.gpa}");
Console.WriteLine($"Student 2: ID={student2.id}, Name={student2.name}, Grade={student2.grade}, GPA={student2.gpa}");
// You can also modify tuple values
var student3 = student1;
student3.name = "Alice Johnson";
Console.WriteLine($"Updated Student: {student3.name}");
}
}
The output of the above code is −
Student 1: ID=101, Name=Alice, Grade=A, GPA=3.8 Student 2: ID=102, Name=Bob, Grade=B, GPA=3.2 Updated Student: Alice Johnson
Returning Named ValueTuples from Methods
Example
using System;
class Program {
static (string name, int age, string city) GetPersonInfo() {
return ("John Doe", 30, "New York");
}
static void Main() {
var person = GetPersonInfo();
Console.WriteLine($"Name: {person.name}");
Console.WriteLine($"Age: {person.age}");
Console.WriteLine($"City: {person.city}");
// You can also deconstruct the tuple
var (personName, personAge, personCity) = GetPersonInfo();
Console.WriteLine($"\nDeconstructed: {personName}, {personAge}, {personCity}");
}
}
The output of the above code is −
Name: John Doe Age: 30 City: New York Deconstructed: John Doe, 30, New York
Conclusion
Named ValueTuples in C# provide a lightweight way to group related data with meaningful field names. They are particularly useful for returning multiple values from methods and creating temporary data structures without defining separate classes.
