Array of Tuples

Q: How would use tuples in C# 4.0 to create an array of tuples containing various types of data (i.e. Employee Name, Employee ID#)?

A: Listed below is a sample code snippet to implement an array of tuples.

//Array of Tuples (i.e. EmpName, EmpIDNum)
Tuple[] EmpRecs =
{
Tuple.Create("Sam Nasr", 891),
Tuple.Create("Jim Smith", 358),
Tuple.Create("Lisa Jones", 962)
};

string FirstEmpName = EmpRecs[0].Item1;

string SecondEmpName = EmpRecs[1].Item1;
int SecondEmpIDNum = EmpRecs[1].Item2;


More information can be found at http://msdn.microsoft.com/en-us/library/dd413854(VS.95).aspx

Comments

Ray said…
Is that JSON?
Sam Nasr said…
No, it's not JSON, it's simply C#.
mab said…
I love tuples but the python syntax is so much simpler and easier than C#.

mytuples = [
('Sam Nasr', 891),
('Jim Smith', 358),
('Lisa Jones', 962)
]

print mytuples[0][0]
print mytuples[1][0]
print mytuples[1][1]

output:
Sam Nasr
Jim Smith
358

Popular posts from this blog

Working with double quotes (") in VB.Net

Using VB Script to display part of an IP address

Sep '19 Announcements