I have the following definition
我有以下定义
static readonly String[,] allItems = {
{ "Alpha", null },
{ "Beta", "Magic" },
{ "Gamma", null },
// more items follow
}
In the first column all items must be initialized to some string literals. In the second column only a small fraction of items must be initialized to string literals and all others are null.
在第一列中,必须将所有项初始化为一些字符串文字。在第二列中,只有一小部分项必须初始化为字符串文字而其他所有项都为空。
I now need to add the third column and it will mostly contain null
s and I'd be happy to omit typing all of them.
我现在需要添加第三列,它将主要包含空值,我很乐意省略所有这些。
Is there a way to somehow omit those null
values?
有没有办法以某种方式省略那些空值?
5 个解决方案
#1
3
I'm not sure if this is the answer you are looking for but have you considered using an object instead. You can then give default values and only supply ones that are different.
我不确定这是否是您正在寻找的答案,但您是否考虑过使用对象。然后,您可以提供默认值,仅提供不同的值。
for example
例如
public class YourObject{
public string Name {get;set;}
public string Value {get;set;}
public string SomethingElse {get;set;}
public YourObject()
{
//provide defaults here
SomethingElse = "";
}
}
Then to create objects you can create like so
然后创建可以像这样创建的对象
static readonly List<YourObject> allItems = new List<YourObject>{
new YourObject{ Name = "Alpha" },
new YourObject{ Name = "Beta", Value = "Magic" },
new YourObject{ Name = "Gamma", SomethingElse = "Hello" }
// more items follow
}
Sriram Sakthivel suggested this whilst i was typing ;)
在我打字的时候,Sriram Sakthivel建议这样做;)
#2
0
Try out list. that would allow you to add only required elements. You can add further elements later on
试试清单。这将允许您只添加必需的元素。您可以稍后添加更多元素
List allList=new List; allList.Add()
列出allList = new List; allList.Add()
#3
0
You can create a class and then use a collection:
您可以创建一个类,然后使用集合:
class MyClass
{
public string Prop1 {get;set;}
public string Prop2 {get;set;}
public MyClass(string prop1, string prop2 = null)
{
this.Prop1 = prop1;
this.Prop2 = prop2
}
}
Now, you can use a List<T>
现在,您可以使用List
var list = new List<MyClass>()
{
new MyClass("Alpha"),
new MyClass("Beta" , "Magic"),
new MyClass("Gamme")
}
#4
0
If you are not sure about number of columns and you want to define just like table then I recommend you using DataTable. Thus you can define column type and enforce any constraint. This way you can even use Linq to manipulate values.
如果您不确定列的数量,并且您想要像表一样定义,那么我建议您使用DataTable。因此,您可以定义列类型并强制执行任何约束。这样你甚至可以使用Linq来操纵值。
Here is the sample code:
以下是示例代码:
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
#5
0
You can write a class modelling a jagged two dimensional array that can be initialized using a collection initializer. The class has to implement IEnumerable
and should have an appropriate Add
method:
您可以编写一个建模锯齿状二维数组的类,该数组可以使用集合初始值设定项进行初始化。该类必须实现IEnumerable并且应该具有适当的Add方法:
class Table<T> : IEnumerable<IEnumerable<T>> {
readonly List<T[]> rows = new List<T[]>();
public void Add(params T[] row) {
rows.Add(row);
}
public IEnumerator<IEnumerable<T>> GetEnumerator() {
return rows.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
You can then use this class directly by say implementing an indexer (public T this[Int32 rowIndex, Int32 columnIndex] { get { ... } }
) but a better solution is probably to create a method that converts the table to an array:
然后你可以直接使用这个类来实现一个索引器(public T this [Int32 rowIndex,Int32 columnIndex] {get {...}})但是更好的解决方案可能是创建一个将表转换为数组的方法:
public T[,] ToArray() {
var columnCount = rows.Max(row => row.Length);
var array = new T[rows.Count, columnCount];
for (var rowIndex = 0; rowIndex < rows.Count; rowIndex += 1)
for (var columnIndex = 0; columnIndex < columnCount; columnIndex += 1) {
var row = rows[rowIndex];
array[rowIndex, columnIndex] = columnIndex < row.Length
? row[columnIndex] : default(T);
}
return array;
}
You can then initialize the two dimensional array using this code:
然后,您可以使用以下代码初始化二维数组:
static readonly String[,] allItems = new Table<String> {
{ "Alpha" },
{ "Beta", "Magic" },
{ "Gamma" },
}.ToArray();
#1
3
I'm not sure if this is the answer you are looking for but have you considered using an object instead. You can then give default values and only supply ones that are different.
我不确定这是否是您正在寻找的答案,但您是否考虑过使用对象。然后,您可以提供默认值,仅提供不同的值。
for example
例如
public class YourObject{
public string Name {get;set;}
public string Value {get;set;}
public string SomethingElse {get;set;}
public YourObject()
{
//provide defaults here
SomethingElse = "";
}
}
Then to create objects you can create like so
然后创建可以像这样创建的对象
static readonly List<YourObject> allItems = new List<YourObject>{
new YourObject{ Name = "Alpha" },
new YourObject{ Name = "Beta", Value = "Magic" },
new YourObject{ Name = "Gamma", SomethingElse = "Hello" }
// more items follow
}
Sriram Sakthivel suggested this whilst i was typing ;)
在我打字的时候,Sriram Sakthivel建议这样做;)
#2
0
Try out list. that would allow you to add only required elements. You can add further elements later on
试试清单。这将允许您只添加必需的元素。您可以稍后添加更多元素
List allList=new List; allList.Add()
列出allList = new List; allList.Add()
#3
0
You can create a class and then use a collection:
您可以创建一个类,然后使用集合:
class MyClass
{
public string Prop1 {get;set;}
public string Prop2 {get;set;}
public MyClass(string prop1, string prop2 = null)
{
this.Prop1 = prop1;
this.Prop2 = prop2
}
}
Now, you can use a List<T>
现在,您可以使用List
var list = new List<MyClass>()
{
new MyClass("Alpha"),
new MyClass("Beta" , "Magic"),
new MyClass("Gamme")
}
#4
0
If you are not sure about number of columns and you want to define just like table then I recommend you using DataTable. Thus you can define column type and enforce any constraint. This way you can even use Linq to manipulate values.
如果您不确定列的数量,并且您想要像表一样定义,那么我建议您使用DataTable。因此,您可以定义列类型并强制执行任何约束。这样你甚至可以使用Linq来操纵值。
Here is the sample code:
以下是示例代码:
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
#5
0
You can write a class modelling a jagged two dimensional array that can be initialized using a collection initializer. The class has to implement IEnumerable
and should have an appropriate Add
method:
您可以编写一个建模锯齿状二维数组的类,该数组可以使用集合初始值设定项进行初始化。该类必须实现IEnumerable并且应该具有适当的Add方法:
class Table<T> : IEnumerable<IEnumerable<T>> {
readonly List<T[]> rows = new List<T[]>();
public void Add(params T[] row) {
rows.Add(row);
}
public IEnumerator<IEnumerable<T>> GetEnumerator() {
return rows.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
You can then use this class directly by say implementing an indexer (public T this[Int32 rowIndex, Int32 columnIndex] { get { ... } }
) but a better solution is probably to create a method that converts the table to an array:
然后你可以直接使用这个类来实现一个索引器(public T this [Int32 rowIndex,Int32 columnIndex] {get {...}})但是更好的解决方案可能是创建一个将表转换为数组的方法:
public T[,] ToArray() {
var columnCount = rows.Max(row => row.Length);
var array = new T[rows.Count, columnCount];
for (var rowIndex = 0; rowIndex < rows.Count; rowIndex += 1)
for (var columnIndex = 0; columnIndex < columnCount; columnIndex += 1) {
var row = rows[rowIndex];
array[rowIndex, columnIndex] = columnIndex < row.Length
? row[columnIndex] : default(T);
}
return array;
}
You can then initialize the two dimensional array using this code:
然后,您可以使用以下代码初始化二维数组:
static readonly String[,] allItems = new Table<String> {
{ "Alpha" },
{ "Beta", "Magic" },
{ "Gamma" },
}.ToArray();