c#引用列表
我想知道:如何向列表中添加新成员,以便在我更改变量值时也会更改列表 例如:c#引用列表,c#,list,C#,List,我想知道:如何向列表中添加新成员,以便在我更改变量值时也会更改列表 例如: int a=4; list<int> l=new list<int>(); l.Add(a); a=5; foreach(var v in l) Console.WriteLine("a="+v); //item class public class Item<T> { T Value {get;set;} } //usage e
int a=4;
list<int> l=new list<int>();
l.Add(a);
a=5;
foreach(var v in l)
Console.WriteLine("a="+v);
//item class
public class Item<T>
{
T Value {get;set;}
}
//usage example
private List<String> items = new List<string>();
public void AddItem( Item<string> item)
{
items.Add(item);
}
public void SetItem(Item<T> item,string value)
{
item.Value=value;
}
inta=4;
列表l=新列表();
l、 添加(a);
a=5;
foreach(l中的v型变量)
控制台写入线(“a=”+v);
输出:
a=4
谢谢如果您想这样做,您需要使用引用类型
使用值类型,例如int
,可以获得列表中变量的副本,而不是引用的副本
请参见MSDN上的。如果希望使用引用类型,则需要使用引用类型
使用值类型,例如int
,可以获得列表中变量的副本,而不是引用的副本
请参见MSDN上的。这不适用于值类型变量列表,每次更改值类型变量时,都会在堆栈中获得新的变量值副本。因此,解决方案是使用某种引用类型的包装器
class NumericWrapper
{
public int Value { get; set; }
}
var items = new List<NumericWrapper>();
var item = new NumericWrapper { Value = 10 };
items.Add(item);
// should be 11 after this line of code
item.Value++;
classnumericwrapper
{
公共int值{get;set;}
}
var items=新列表();
var item=newnumericwrapper{Value=10};
项目。添加(项目);
//这行代码后面应该是11
item.Value++;
这不适用于值类型变量列表,每次更改值类型变量时,都会在堆栈中获得新的变量值副本。因此,解决方案是使用某种引用类型的包装器
class NumericWrapper
{
public int Value { get; set; }
}
var items = new List<NumericWrapper>();
var item = new NumericWrapper { Value = 10 };
items.Add(item);
// should be 11 after this line of code
item.Value++;
classnumericwrapper
{
公共int值{get;set;}
}
var items=新列表();
var item=newnumericwrapper{Value=10};
项目。添加(项目);
//这行代码后面应该是11
item.Value++;
您可以构建一个包装器容器,然后根据需要更新包装器的值。例如,如下所示:
int a=4;
list<int> l=new list<int>();
l.Add(a);
a=5;
foreach(var v in l)
Console.WriteLine("a="+v);
//item class
public class Item<T>
{
T Value {get;set;}
}
//usage example
private List<String> items = new List<string>();
public void AddItem( Item<string> item)
{
items.Add(item);
}
public void SetItem(Item<T> item,string value)
{
item.Value=value;
}
//项目类
公共类项目
{
T值{get;set;}
}
//用法示例
私有列表项=新列表();
公共无效附加项(项目)
{
项目。添加(项目);
}
公共void SetItem(项,字符串值)
{
项目价值=价值;
}
您可以构建一个包装器容器,然后根据需要更新包装器的值。例如,如下所示:
int a=4;
list<int> l=new list<int>();
l.Add(a);
a=5;
foreach(var v in l)
Console.WriteLine("a="+v);
//item class
public class Item<T>
{
T Value {get;set;}
}
//usage example
private List<String> items = new List<string>();
public void AddItem( Item<string> item)
{
items.Add(item);
}
public void SetItem(Item<T> item,string value)
{
item.Value=value;
}
//项目类
公共类项目
{
T值{get;set;}
}
//用法示例
私有列表项=新列表();
公共无效附加项(项目)
{
项目。添加(项目);
}
公共void SetItem(项,字符串值)
{
项目价值=价值;
}
必须将int包装在引用类型中
试试这个:
internal class Program
{
private static void Main(string[] args)
{
IntWrapper a = 4;
var list = new List<IntWrapper>();
list.Add(a);
a.Value = 5;
//a = 5; //Dont do this. This will assign a new reference to a. Hence changes will not reflect inside list.
foreach (var v in list)
Console.WriteLine("a=" + v);
}
}
public class IntWrapper
{
public int Value;
public IntWrapper()
{
}
public IntWrapper(int value)
{
Value = value;
}
// User-defined conversion from IntWrapper to int
public static implicit operator int(IntWrapper d)
{
return d.Value;
}
// User-defined conversion from int to IntWrapper
public static implicit operator IntWrapper(int d)
{
return new IntWrapper(d);
}
public override string ToString()
{
return Value.ToString();
}
}
内部类程序
{
私有静态void Main(字符串[]args)
{
inta=4;
var list=新列表();
列表.添加(a);
a、 数值=5;
//a=5;//不要这样做。这将为a分配一个新的引用。因此更改不会反映在列表中。
foreach(列表中的var v)
控制台写入线(“a=”+v);
}
}
公共类IntWrapper
{
公共价值观;
公共IntWrapper()
{
}
公共IntWrapper(int值)
{
价值=价值;
}
//从IntWrapper到int的用户定义转换
公共静态隐式运算符int(intd)
{
返回d.值;
}
//从int到IntWrapper的用户定义转换
公共静态隐式运算符IntWrapper(intd)
{
返回新的IntWrapper(d);
}
公共重写字符串ToString()
{
返回值.ToString();
}
}
必须将int包装在引用类型中
试试这个:
internal class Program
{
private static void Main(string[] args)
{
IntWrapper a = 4;
var list = new List<IntWrapper>();
list.Add(a);
a.Value = 5;
//a = 5; //Dont do this. This will assign a new reference to a. Hence changes will not reflect inside list.
foreach (var v in list)
Console.WriteLine("a=" + v);
}
}
public class IntWrapper
{
public int Value;
public IntWrapper()
{
}
public IntWrapper(int value)
{
Value = value;
}
// User-defined conversion from IntWrapper to int
public static implicit operator int(IntWrapper d)
{
return d.Value;
}
// User-defined conversion from int to IntWrapper
public static implicit operator IntWrapper(int d)
{
return new IntWrapper(d);
}
public override string ToString()
{
return Value.ToString();
}
}
内部类程序
{
私有静态void Main(字符串[]args)
{
inta=4;
var list=新列表();
列表.添加(a);
a、 数值=5;
//a=5;//不要这样做。这将为a分配一个新的引用。因此更改不会反映在列表中。
foreach(列表中的var v)
控制台写入线(“a=”+v);
}
}
公共类IntWrapper
{
公共价值观;
公共IntWrapper()
{
}
公共IntWrapper(int值)
{
价值=价值;
}
//从IntWrapper到int的用户定义转换
公共静态隐式运算符int(intd)
{
返回d.值;
}
//从int到IntWrapper的用户定义转换
公共静态隐式运算符IntWrapper(intd)
{
返回新的IntWrapper(d);
}
公共重写字符串ToString()
{
返回值.ToString();
}
}