手动填写数据类型集合的C#列表
我正在编写一个C#示例来学习列表以及如何初始化它们。所以我有一个具有一些属性的类:手动填写数据类型集合的C#列表,c#,list,properties,C#,List,Properties,我正在编写一个C#示例来学习列表以及如何初始化它们。所以我有一个具有一些属性的类: public class C1 { public string p1 { get; set; } public string p2 { get; set; } public List<StatusChoices> ChoiceList { get; set; } ... public string FillList(string v1, int v2, bo
public class C1
{
public string p1 { get; set; }
public string p2 { get; set; }
public List<StatusChoices> ChoiceList { get; set; }
...
public string FillList(string v1, int v2, bool v3)
{
// How can I fill this.ChoiceList?
this.ChoiceList.val1 = v1; //Is this possible?
return this.ChoiceList.val1;
}
}
public class StatusChoices
{
public string val1 { get; set; }
public int val2 { get; set; }
public bool val3 { get; set; }
}
公共类C1
{
公共字符串p1{get;set;}
公共字符串p2{get;set;}
公共列表选择列表{get;set;}
...
公共字符串填充列表(字符串v1、int v2、bool v3)
{
//我怎么填这个,唱诗班?
this.ChoiceList.val1=v1;//这可能吗?
返回this.ChoiceList.val1;
}
}
公共类状态选择
{
公共字符串val1{get;set;}
公共int val2{get;set;}
公共布尔值3{get;set;}
}
这可能很容易,但到目前为止我还没能做到。如何将一些值“手动添加到列表中?只需创建StatusChoices类的实例,初始化它并将其添加到列表中即可 (我添加了列表的延迟初始化)
公共类C1{
公共字符串p1{get;set;}
公共字符串p2{get;set;}
公共列表选择列表{get;set;}
...
公共字符串填充列表(字符串v1、int v2、bool v3){
if(ChoiceList==null)
{
this.ChoiceList=新列表();
}
var newItem=newstatuschoices{val1=v1,val2=v2,val3=v3};
this.ChoiceList.Add(newItem);
返回newItem.val1;
}
}
只需创建StatusChoices类的实例,初始化它并将其添加到列表中即可
(我添加了列表的延迟初始化)
公共类C1{
公共字符串p1{get;set;}
公共字符串p2{get;set;}
公共列表选择列表{get;set;}
...
公共字符串填充列表(字符串v1、int v2、bool v3){
if(ChoiceList==null)
{
this.ChoiceList=新列表();
}
var newItem=newstatuschoices{val1=v1,val2=v2,val3=v3};
this.ChoiceList.Add(newItem);
返回newItem.val1;
}
}
创建类C1
时,选项列表
初始化为null
。(事实上,每个类字段(包括隐藏属性支持字段)都初始化为其默认值。)必须显式创建列表对象
您可以在类构造函数中执行此操作
public class C1
{
...
public List<StatusChoices> ChoiceList { get; set; }
public C1() // Constructor. Is run when a new C1 object is created with `new`.
{
ChoiceList = new List<StatusChoices>();
}
...
}
请注意,您还必须创建一个StatusChoices
对象。可以将类视为可用于创建对象的模板
将对象添加到列表后,可以通过枚举访问它们
foreach (StatusChoices sc in ChoiceList) {
Console.WriteLine($"val 1 = {sc.val1}, val 2 = {sc.val2}, val 3 = {sc.val3}")
}
或者通过索引
StatusChoices first = ChoiceList[0];
StatusChoices second = ChoiceList[1];
string v1_of_first = first.val1;
string v1_of_second = second.val1;
您还可以直接访问元素的属性
string v1_of_first = ChoiceList[0].val1;
string v1_of_second = ChoiceList[1].val1;
如果你有一个对象C1=newc1()代码>,您可以编写
string v = c1.ChoiceList[0].val1;
您甚至可以构建C1
对象的列表
var mainList = new List<C1>(); // The var-keyword lets C# infer the type.
// This saves you from writing List<C1> again.
mainList.Add(new C1());
mainList.Add(new C1());
mainList.Add(new C1());
mainList[0].FillList("a", 12, true);
mainList[0].FillList("b", 33, false);
mainList[1].FillList("x", 77, true);
...
string v = mainList[1].ChoiceList[2].val1;
短版本使用对象初始值设定项{val1=v1,…}
创建类C1
时,选项列表
被初始化为null
。(事实上,每个类字段(包括隐藏属性支持字段)都初始化为其默认值。)必须显式创建列表对象
您可以在类构造函数中执行此操作
public class C1
{
...
public List<StatusChoices> ChoiceList { get; set; }
public C1() // Constructor. Is run when a new C1 object is created with `new`.
{
ChoiceList = new List<StatusChoices>();
}
...
}
请注意,您还必须创建一个StatusChoices
对象。可以将类视为可用于创建对象的模板
将对象添加到列表后,可以通过枚举访问它们
foreach (StatusChoices sc in ChoiceList) {
Console.WriteLine($"val 1 = {sc.val1}, val 2 = {sc.val2}, val 3 = {sc.val3}")
}
或者通过索引
StatusChoices first = ChoiceList[0];
StatusChoices second = ChoiceList[1];
string v1_of_first = first.val1;
string v1_of_second = second.val1;
您还可以直接访问元素的属性
string v1_of_first = ChoiceList[0].val1;
string v1_of_second = ChoiceList[1].val1;
如果你有一个对象C1=newc1()代码>,您可以编写
string v = c1.ChoiceList[0].val1;
您甚至可以构建C1
对象的列表
var mainList = new List<C1>(); // The var-keyword lets C# infer the type.
// This saves you from writing List<C1> again.
mainList.Add(new C1());
mainList.Add(new C1());
mainList.Add(new C1());
mainList[0].FillList("a", 12, true);
mainList[0].FillList("b", 33, false);
mainList[1].FillList("x", 77, true);
...
string v = mainList[1].ChoiceList[2].val1;
短版本使用对象初始值设定项{val1=v1,…}
类似这样的内容
public string FillList(string v1, int v2, bool v3){
this.ChoiceList = new List<StatusChoices> {
new StatusChoices { val1 = v1, val2 = v2, val3 = v3, } };
return this.ChoiceList[0].val1;
}
公共字符串填充列表(字符串v1、int v2、bool v3){
this.ChoiceList=新列表{
新的状态选择{val1=v1,val2=v2,val3=v3,};
返回此。ChoiceList[0]。val1;
}
类似这样的东西
public string FillList(string v1, int v2, bool v3){
this.ChoiceList = new List<StatusChoices> {
new StatusChoices { val1 = v1, val2 = v2, val3 = v3, } };
return this.ChoiceList[0].val1;
}
公共字符串填充列表(字符串v1、int v2、bool v3){
this.ChoiceList=新列表{
新的状态选择{val1=v1,val2=v2,val3=v3,};
返回此。ChoiceList[0]。val1;
}
请参阅。您需要使用向列表中添加元素的方法之一,最直接的方法是add(StatusChoices项)
。您只能添加StatusChoices
类的实例,因为您专门化了ChoiceList
。如果您声明了类型列表的变量,则只能向其添加类型为StatusChoices的对象的实例。只需阅读中的Add方法,请参阅。您需要使用向列表中添加元素的方法之一,最直接的方法是add(StatusChoices项)
。您只能添加StatusChoices
类的实例,因为您专门化了ChoiceList
。如果您声明了类型列表的变量,则只能向其添加类型为StatusChoices的对象的实例。只要阅读一下@olivier jacot descombes中关于Add方法的详细解释,谢谢olivier!干得好@olivier jacot descombes详细解释谢谢olivier!