hi all im new to c# and ned a bit of help i want a list containing pairs of string not uniqe for example
mylist.add("bob", "steve");
mylist.add("bob", "ben");
mylist.add("ben", "steve");
and would contain
mylist[0] bob steve
mylist[1] bob ben
mylist[2] ben steve
how would i do this in code thanks.
Loading

Laurentiu Popovici2 reputationPosted Jul 10, 2010, 11:41 AM EDT
http://blog.dynamicprogrammer.com/2009/05/25/TupleANewTypeOnNet40.aspx
Otherwise, you can use List
>.
List
> myList = new List
>(); listTwoStrings = new List(2);
List
listTwoStrings.Add("Bob");
listTwoStrings.Add("Steve");
myList.Add(listTwoStrings);
Ajay Gautam84 reputationPosted Jul 10, 2010, 11:50 AM EDT
Please find the example, hope it helps you. Just understand the logic of List because it list can save only 1 value at a time.
using System; using System.Collections.Generic; class Program { static void Main() { List cities = new List(); // List of city names cities.Add("San Diego"); // String element 1 cities.Add("Humboldt"); // 2 cities.Add("Los Angeles"); // 3 cities.Add("Auburn"); // 4 // Write each city string. foreach (string city in cities) { Console.WriteLine(city); } } } --- Output of the program --- San Diego Humboldt Los Angeles Auburndave hicks5 reputationPosted Jul 10, 2010, 11:55 AM EDT
dave hicks5 reputationPosted Jul 10, 2010, 11:57 AM EDT
Roy S194 reputationPosted Jul 10, 2010, 12:15 PM EDT
http://www.c-sharpcorner.com/UploadFile/mahesh/2002/
Or maybe you could just add the first and last name together, separated by a space and store that in a list?
theLizardRank #1,079 · 1.8K reputationPosted Jul 10, 2010, 1:49 PM EDT
public class namepairs
{
public string a, b;
public namepairs(string first, string second)
{
a = first; b = second;
}
}
List
myList.Add(new namepairs("bob", "steve"));
myList.Add(new namepairs("bob", "ben"));
myList.Add(new namepairs("ben", "steve"));
Sam HobbsRank #107 · 20.5K reputationPosted Jul 10, 2010, 9:42 PM EDT
I do not understand the question, though; would the above work? If not and you still need help then please explain.