简体   繁体   English

C# 中的通用 object

[英]Generic object in C#

I'm new with C#.我是 C# 的新手。 In vb.net I can have a list of object and I can add the new one with the next lines:在 vb.net 我可以有一个 object 的列表,我可以在下一行添加新的:

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#?知道如何在 C# 中做到这一点吗? Without create a specific class无需创建特定的 class

Thanxs谢谢

In c#?在 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.实际上:只需在此处声明您明确想要的 class 即可。 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>并使用List<Something>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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