根据MSDN,如果所有数组都是引用类型,那么为什么在给定的示例代码中,t2的新值不能反映t1的变化?
string[] data = new[] { "One","Two" }; var t1 = data[0]; Console.WriteLine(t1); var t2 = t1; t2 = "Three"; //assigning the new value,and this should reflect in t1 Console.WriteLine(t2); Console.WriteLine(t1); // this should print 'Three',but it prints 'One' Console.Read();
http://msdn.microsoft.com/en-us/magazine/cc301755.aspx
Arrays are mechanisms that allow you to treat several items as a
single collection. The Microsoft® .NET Common Language Runtime (CLR)
supports single-dimensional arrays,multidimensional arrays,and
jagged arrays (arrays of arrays). All array types are implicitly
derived from System.Array,which itself is derived from System.Object.
This means that all arrays are always reference types which are
allocated on the managed heap,and your app’s variable contains a
reference to the array and not the array itself.
解决方法
原文地址:https://www.jb51.cc/csharp/98786.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。