Darcy Darcy - 1 month ago 10
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

List
that has multiple parameters without creating a class.

Example:

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

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).