Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何覆盖列表中的特定项目<;对象>;_C# - Fatal编程技术网

C# 如何覆盖列表中的特定项目<;对象>;

C# 如何覆盖列表中的特定项目<;对象>;,c#,C#,我有一个具有特定值的项目列表。当我的列表在代码中传递时,这些值可能需要更改。我理解插入和添加,但是否有一些方法或技术可以更新或插入?如果需要替换引用,只需分配它即可 list[index] = new Item(); 如果需要更新该项的属性,则需要强制转换它 var item = list[index] as Item; item.Foo = bar; 我强烈建议您使用泛型列表的特定类型-因此列表,您不需要强制转换出来的项目。如果您需要替换引用,只需指定它即可 list[index] = n

我有一个具有特定值的项目列表。当我的列表在代码中传递时,这些值可能需要更改。我理解插入和添加,但是否有一些方法或技术可以更新或插入?

如果需要替换引用,只需分配它即可

list[index] = new Item();
如果需要更新该项的属性,则需要强制转换它

var item = list[index] as Item;
item.Foo = bar;

我强烈建议您使用泛型列表的特定类型-因此
列表
,您不需要强制转换出来的项目。

如果您需要替换引用,只需指定它即可

list[index] = new Item();
如果需要更新该项的属性,则需要强制转换它

var item = list[index] as Item;
item.Foo = bar;

我强烈建议您使用通用列表的特定类型-因此
列表
,您不需要强制转换出来的项目。

您可以使用带有列表的索引器来访问特定项目

var item = list[0];
更改列表中的项目

list[0] = someobject;
假设我们有一个字符串列表

 List<string> lst = new List<string>();
 lst.Add("Hello");
 lst[0] = "Change hello";
List lst=new List();
加上(“你好”);
lst[0]=“更改你好”;

您可以使用带有列表的索引器访问特定项目

var item = list[0];
更改列表中的项目

list[0] = someobject;
假设我们有一个字符串列表

 List<string> lst = new List<string>();
 lst.Add("Hello");
 lst[0] = "Change hello";
List lst=new List();
加上(“你好”);
lst[0]=“更改你好”;

在这种情况下,您只需要更改给定索引处的项目。在该索引上使用赋值运算符,如

   list[index] = MyOtherReferenceOfListsType;

在这种情况下,您只需要更改给定索引处的项。在该索引上使用赋值运算符,如

   list[index] = MyOtherReferenceOfListsType;
列表中有
Insert(int-index,T项)
,而不是
InsertAt
列表中有
Insert(int-index,T项)
,而不是
InsertAt