简体   繁体   中英

Generic object in C#

I'm new with C#. In vb.net I can have a list of object and I can add the new one with the next lines:

Dim lObject = new list (of object)
lObject.add(new with{.id = 1, .name ="hello"})

After that I can access every row of list and every field.

Any idea how can I do that in C#? Without create a specific class

Thanxs

In c#? You could cheat with value-tuples:

var list = new List<(int id, string name)>();
list.Add((1, "hello"));

(Although technically these aren't objects until boxed, but: it'll do the job)

In reality: just declare the class that you clearly want here. It'll save you a lot of pain:

class Something {
    public int Id {get;set;}
    public string Name {get;set;}
}

and use a List<Something>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM