声明一个列表并使用一个代码语句填充值 [英] Declare a List and populate with values using one code statement

查看:68
本文介绍了声明一个列表并使用一个代码语句填充值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

var list = new List<IMyCustomType>();
list.Add(new MyCustomTypeOne());
list.Add(new MyCustomTypeTwo());
list.Add(new MyCustomTypeThree());

这当然可以,但是我想知道:如何声明列表并使用一个语句用值填充它?

this of course works, but I'm wondering: how can I declare the list and populate it with values using one statement?

谢谢

推荐答案

var list = new List<IMyCustomType>{ 
    new MyCustomTypeOne(), 
    new MyCustomTypeTwo(), 
    new MyCustomTypeThree() 
};

Asker将一行"更改为一条语句",这看起来更好.

Asker changed "one line" to "one statement", and this looks nicer.

这篇关于声明一个列表并使用一个代码语句填充值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆