我无法理解背后的逻辑,List<int>因为它打破了一些基本规则.
List<int> 应该是值类型而不是引用类型.
List<int>ref如果必须在函数调用之间保持其值,则必须通过关键字传递.所以这意味着它正在显示类似于int的值类型行为.List<int>必须由新的运营商初始化.也List<int>可以为null.这意味着引用类型行为.可空类型是不同的,因为它不必由新运算符初始化.
我在这看错了吗?
EDITED-
我应该在原始问题中发布代码.但它遵循 -
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ListTest d = new ListTest();
d.Test();
}
}
class ListTest
{
public void ModifyIt(List<int> l)
{
l = returnList();
}
public void Test()
{
List<int> listIsARefType = new List<int>();
ModifyIt(listIsARefType);
Console.WriteLine(listIsARefType.Count); // should have been 1 but is 0
Console.ReadKey(true);
}
public List<int> returnList()
{
List<int> t = new List<int>();
t.Add(1); …Run Code Online (Sandbox Code Playgroud)