通过引用或值初始化的C#容器? [英] C# containers initialized by reference or value?

查看:71
本文介绍了通过引用或值初始化的C#容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++很有经验,但是对C#有点新知识。



将对象添加到容器时,它们是通过引用还是通过值传递的? / p>

也就是说,如果我这样做:

  myClass m = new myClass(0); //假设该类仅包含一个int 
List< myClass> myList =新列表< myClass>(1);
myList.Add(m);
myList [0] + = 1;
Console.WriteLine(m);
Console.WriteLine(myList [0]);

结果将是:

  0 
1

还是会

  1 
1



如果是前者,那我怎样才能做到后者呢?我的第一个直觉是做类似

  myClass ref mref = m的事情; 
Console.WriteLine(mref);

但这似乎不是有效的语法。

解决方案

该值按值传递给 Add 方法;但是,如果传递引用类型(类始终是引用类型),则该值本身就是引用。因此,问题不只是值是通过值传递还是通过引用传递,而是类型是值类型还是引用类型。

  class MyClass 
{
public int Number {get;组; }
}

通过此声明,我们得到:

  MyClass m = new MyClass(); 
List< MyClass> myList = new List< MyClass>();
myList.Add(m);

myList [0] .Number + = 1;
Console.WriteLine(myList [0] .Number); //显示1个
Console.WriteLine(m.Number); //显示1

myList [0]。数字+ = 1;
Console.WriteLine(myList [0] .Number); //显示2个
Console.WriteLine(m.Number); //显示2

请注意,这不适用于结构,因为 myList [0] 返回的值将是存储在列表中的值的副本。 + = 1 只会增加此临时副本的 Number 属性,因此除了消耗一些属性外没有其他效果处理器周期。因此,最好只创建不可变的结构。






如果要直接显示对象,请覆盖 ToString

  class MyClass 
{
public int Number {得到;组; }

公共替代字符串ToString()
{
return Number.ToString();
}
}

现在,您可以写

  myList [0]。数字+ = 1; 
Console.WriteLine(myList [0]);
Console.WriteLine(m);






您甚至可以制作 myList [0] + = 1 与运算符重载一起使用。在 MyClass 中声明

 公共静态MyClass运算符+(MyClass m,int i)
{
m.Number + = i;
return m;
}

但这有点奇怪,除非您的班级代表一个数字,但是在在这种情况下,将首选不可变的 struct ,因为通常将数字视为不可变的值类型。

 公共静态MyStruct运算符+(MyStruct m,int i)
{
返回new MyStruct(m.Number + i);
}


I'm experienced with C++, but a little new to C#.

When you add objects to a container, are they passed by reference or by value?

That is, if I do this:

myClass m = new myClass(0);       //Assume the class just holds an int
List<myClass> myList = new List<myClass>(1);
myList.Add(m);
myList[0] += 1;
Console.WriteLine(m);
Console.WriteLine(myList[0]);

Will the result be:

0
1

or will it be

1
1

?

If the former, then how can get I make it do the latter? My first instinct was to do something like

myClass ref mref = m; 
Console.WriteLine(mref);

But this doesn't seem to be valid syntax.

解决方案

The value is passed by value to the Add method; however, if you pass a reference type (a class is always a reference type), then the value itself is a reference. So the question is not so much whether the value is passed by value or by reference, but if the type is a value type or a reference type.

class MyClass
{
    public int Number { get; set; }
}

With this declaration, we get:

MyClass m = new MyClass();
List<MyClass> myList = new List<MyClass>();
myList.Add(m);

myList[0].Number += 1;
Console.WriteLine(myList[0].Number); // Displays 1
Console.WriteLine(m.Number); // Displays 1

myList[0].Number += 1;
Console.WriteLine(myList[0].Number); // Displays 2
Console.WriteLine(m.Number); // Displays 2

Note that this would not work with a struct, because the value returned by myList[0] would be a copy of the value stored in the list. The += 1 would only increment the Number property of this temporary copy and thus have no other effect than consuming a few processor cycles. Therefore it is a good advice to create only immutable structs.


If you want to display the object directly, override ToString

class MyClass
{
    public int Number { get; set; }

    public override string ToString()
    {
        return Number.ToString();
    }
}

Now, you can write

myList[0].Number += 1;
Console.WriteLine(myList[0]);
Console.WriteLine(m);


You could even make myList[0] += 1 work with an operator overload. In MyClass declare

public static MyClass operator +(MyClass m, int i) 
{
    m.Number += i;
    return m;
}

But this is a bit weird, unless your class represents a number, but in that case an immutable struct would be preferred, as numbers are generally perceived as immutable value types.

public static MyStruct operator +(MyStruct m, int i) 
{
    return new MyStruct(m.Number + i);
}

这篇关于通过引用或值初始化的C#容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆