Darcy Darcy - 1 year ago 134
C# Question

Multiple params in a List in C#. How to create without a class?

This is probably a pretty obvious question, but how would I go about creating a

that has multiple parameters without creating a class.


var list = new List<string, int>();

list.Add("hello", 1);

I normally would use a class like so:

public class MyClass
public String myString {get; set;}
public Int32 myInt32 {get; set;}

then create my list by doing:

var list = new List<MyClass>();
list.Add(new MyClass { myString = "hello", myInt32 = 1 });

Answer Source

If you are using .NET 4.0 you can use a Tuple.

List<Tuple<T1, T2>> list;

For older versions of .NET you have to create a custom class (unless you are lucky enough to be able to find a class that fits your needs in the base class library).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download