我想知道如何证明 .Net 框架在幕后做了什么。
我有一个接受 List<String> originalParameterList
参数的方法.
在我的方法中我有另一个 List<String> newListObj
如果我执行以下操作:
List<String> newListObj = originalParameterList
newListObj.Add(value);
newListObj.Add(value1);
newListObj.Add(value2);
The count of the originalParameterList grows (+3).
如果我这样做:
List<String> newListObj = new List<String>(originalParamterList);
newListObj.Add(value);
newListObj.Add(value1);
newListObj.Add(value2);
The count of the originalParameterList stays the sames (+0).
我还发现这段代码的行为相同:
List<String> newListObj = new List<String>(originalParamterList.ToArray());
newListObj.Add(value);
newListObj.Add(value1);
newListObj.Add(value2);
The count of the originalParameterList stays the sames (+0).
我的问题是,有没有办法以明确的方式了解 .Net Framework 在幕后做了什么?
最佳答案
您可以将程序集加载到 ILDASM 中并(加载后)找到您的方法并双击它, 它将显示该方法的 cil 代码。只需在搜索的 Windows 开始菜单中键入“IL”即可。
或者你可以使用以下这些方法来创建一个新的独立列表
private void GetList(List<string> lst)
{
List<string> NewList = lst.Cast<string>().ToList();
NewList.Add("6");
//not same values.
//or....
List<string> NewList = lst.ConvertAll(s => s);
NewList.Add("6");
//again different values
}
关于c# - List<String> ByRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17368575/