如何设置集合内联? [英] How to set collection inline?
本文介绍了如何设置集合内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
数据表表=新的DataTable()
{
列=新DataColumnCollection(
{
新的DataColumn(COL1),
新的DataColumn(COL2)
})
});
解决方案
您说的是集合初始化器 功能添加C#3,像这样做:
数据表表=新的DataTable()
{
列=
{
新的DataColumn(COL1),
新的DataColumn(COL2)
}
};
这不叫收藏的构造,它使用已经存在于数据表的集合。
这是短手Columns.Add(),所以它不需要列有一个二传手。
您是如此接近与code。在您的问题!
For example:
DataTable table = new DataTable()
{
Columns = new DataColumnCollection(
{
new DataColumn("col1"),
new DataColumn("col2")
})
});
解决方案
You are talking about the Collection Initialiser feature added in C# 3. It is done like this:
DataTable table = new DataTable()
{
Columns =
{
new DataColumn("col1"),
new DataColumn("col2")
}
};
This does not call a collection constructor, it uses the collection which already exists in the DataTable.
This is short-hand for Columns.Add(), so it doesn't require Columns to have a setter.
You were so close with the code in your question!
这篇关于如何设置集合内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文