AliAzra
AliAzra

Reputation: 919

How to use two types in list List<T,T2>

Can we use two different type argument in List <T> or add one more argument inside the class type?

For example:

public class ClassA
{
 public int a {get;set;}
 public int b {get;set;}
}

I need on more properties in the List without editing ClassA

public string c {get;set;}

public List<ClassA, c> list = new List<ClassA, c>

I should able to access c inside the List

item[0].a
item[0].b
item[0].c

Upvotes: 0

Views: 571

Answers (3)

AliAzra
AliAzra

Reputation: 919

Thanks for help guys it was a great help. but in my cod i used new class:

public classB 
{
     public classA cA {get;set;}
     public str c {get;set;}
}

List<B>  i can access any data i need it. 

Upvotes: -1

Xeorge Xeorge
Xeorge Xeorge

Reputation: 462

Dictionary<String,ClassA> myDictionary= New Dictionary<String,ClassA>();

Foreach(var k in myDictionary.Keys)
Debug.WriteLine(myDictionary[k]);// accesses (Value) ClassA objects

Upvotes: 0

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31143

If you are using a recent C# version you can use tuples:

public List<(ClassA, string)> list = new List<(ClassA, string)>

You can also name them

public List<(ClassA a, string c)> list = new List<(ClassA a, string c)>

But you can’t access anything inside ClassA directly, you’ll have to go with the first object’s name.

Upvotes: 7

Related Questions