I am getting confused to understand the multiple dimensional array. I am having three data(strFname,strLname,strMname).
我很难理解多维数组。我有三个数据(strFname,strLname,strMname)。
I need to put this data in a multi dimensional array.There could be n number of rows. But for each row these three data I need to add.
我需要将这些数据放在一个多维数组中。可能有n行。但是对于每一行,我需要添加这三个数据。
Any useful reference is welcome.
欢迎任何有用的参考。
3 个解决方案
#1
Checkout this link: http://www.functionx.com/csharp/Lesson23.htm
查看此链接:http://www.functionx.com/csharp/Lesson23.htm
#2
That could be a string[,]
:
那可能是一个字符串[,]:
string[,] data = new string[4, 3] {
{"a","b","c"},
{"d","e","f"},
{"g","h","i"},
{"j","k","l"}
};
However, I would advise you create a type with the expected properties, and have a List<T>
of that type - much easier to understand:
但是,我建议你创建一个具有预期属性的类型,并且具有该类型的List
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}
...
List<Person> people = new List<Person>();
people.Add(new Person {FirstName = "Fred", ... });
#3
I don't think you should use a multi-dimensional array here - I think you should encapsulate the three values in a type - something like this (assuming I've interpreted the names correctly):
我认为你不应该在这里使用多维数组 - 我认为你应该将三个值封装在一个类型中 - 就像这样(假设我已正确解释了这些名称):
public sealed class Name
{
private readonly string firstName;
private readonly string lastName;
private readonly string middleName;
// Consider changing the ordering here?
public Name(string firstName, string lastName, string middleName)
{
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
}
public string FirstName
{
get { return firstName; }
}
public string LastName
{
get { return lastName; }
}
public string MiddleName
{
get { return middleName; }
}
}
Then you can use a List<Name>
or a Name[]
, both of which make the purpose clear.
然后,您可以使用List
(Yeah, this is now basically the same as Marc's answer, except my type is immutable :)
(是的,现在这与Marc的答案基本相同,除了我的类型是不可变的:)
#1
Checkout this link: http://www.functionx.com/csharp/Lesson23.htm
查看此链接:http://www.functionx.com/csharp/Lesson23.htm
#2
That could be a string[,]
:
那可能是一个字符串[,]:
string[,] data = new string[4, 3] {
{"a","b","c"},
{"d","e","f"},
{"g","h","i"},
{"j","k","l"}
};
However, I would advise you create a type with the expected properties, and have a List<T>
of that type - much easier to understand:
但是,我建议你创建一个具有预期属性的类型,并且具有该类型的List
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}
...
List<Person> people = new List<Person>();
people.Add(new Person {FirstName = "Fred", ... });
#3
I don't think you should use a multi-dimensional array here - I think you should encapsulate the three values in a type - something like this (assuming I've interpreted the names correctly):
我认为你不应该在这里使用多维数组 - 我认为你应该将三个值封装在一个类型中 - 就像这样(假设我已正确解释了这些名称):
public sealed class Name
{
private readonly string firstName;
private readonly string lastName;
private readonly string middleName;
// Consider changing the ordering here?
public Name(string firstName, string lastName, string middleName)
{
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
}
public string FirstName
{
get { return firstName; }
}
public string LastName
{
get { return lastName; }
}
public string MiddleName
{
get { return middleName; }
}
}
Then you can use a List<Name>
or a Name[]
, both of which make the purpose clear.
然后,您可以使用List
(Yeah, this is now basically the same as Marc's answer, except my type is immutable :)
(是的,现在这与Marc的答案基本相同,除了我的类型是不可变的:)