简体   繁体   English

C#构造函数,是否通过引用?

[英]C# Constructor, Pass reference or not?

Looking at this Microsoft article How to: Write a Copy Constructor (C#) and also this Generic C# Copy Constructor , wouldn't it be best/safe to use a reference to the class instance than to use a plain copy of the instance ? 查看此Microsoft文章如何:编写副本构造函数(C#)以及此通用C#副本构造函数 ,使用对类实例的引用比使用实例的纯副本,不是最好/最安全的方法吗?

public class Myclass()
{
    private int[] row;
    public MyClass(ref MyClass @class)
    {
        for(int i = 0; i<@class.row.Length;i++)
        {
            this.row[i] = @class.row[i];
        }
    }
}

What ref actually means: ref实际含义是:

void Change(SomeClass instance)
{
    instance = new SomeClass();
}
void ChangeRef(ref SomeClass instance)
{
    instance = new SomeClass();
}

Later... 后来...

SomeClass instance = new SomeClass();
Change(instance);
//value of instance remains unchanged here
ChangeRef(ref instance);
//at this line, instance has been changed to a new instance because
//ref keyword imports the `instance` variable from the call-site's scope

I can't see how this functionality would be useful with respect to a copy constructor. 我看不到此功能对复制构造函数的用处。

Object by nature is reference not a value type. 对象本质上是引用,而不是值类型。 I do not see any good reason what extra advantage you would get doing it. 我看不出有什么充分的理由可以使您获得更多好处。 But yes you might get into problems because of it, consider this - 但是,是的,您可能因此而陷入麻烦,请考虑一下-

You created an object and passed it with reference to couple of classes and those classes are now having access to the address of reference itself. 您创建了一个对象,并将其传递给几个类的引用,这些类现在可以访问引用本身的地址。 Now I have got all the powers to go and change the reference itself with another object's reference. 现在,我拥有了一切权力去用另一个对象的引用更改引用本身。 If here, another class had this object it is actually working on some stale object and other classes can not see what changes are being made and you are in chaos. 如果在这里,另一个类具有此对象,则该对象实际上正在某个过时的对象上工作,而其他类则看不到正在进行的更改并且您处于混乱状态。

I do not see any use of doing it, rather it is dangerous. 我看不到这样做有什么用,但是很危险。 It does not sounds like a OO way of writing code to me. 这听起来不像是一种面向对象的向我编写代码的方式。

The ref keyword is used when a method should be allowed to change the location of a reference. 当应允许某个方法更改引用的位置时,使用ref关键字。 Reference types always pass their reference into a method (but the location of the reference cannot be modified via assignment). 引用类型始终将其引用传递给方法(但是不能通过赋值来修改引用的位置)。 Values types pass their value. 值类型传递其值。

See: Passing Parameters 请参阅: 传递参数

Example: 例:

void PassingByReference(List<int> collection)
{
    // Compile error since method cannot change reference location
    // collection = new List<int>();
    collection.Add(1);
}

void ChangingAReference(ref List<int> collection)
{
    // Allow to change location of collection with ref keyword
    collection = new List<int>();
    collection.Add(2);
}

var collection = new List<int>{ 5 };

// Pass the reference of collection to PassByReference
PassingByReference(collection);

// collection new contains 1
collection.Contains(5); // true
collection.Contains(1); // true

// Copy the reference of collection to another variable
var tempCollection = collection;

// Change the location of collection via ref keyword
ChangingAReference(ref collection);

// it is not the same collection anymore
collection.Contains(5); // false
collection.Contains(1); // false

// compare the references use the default == operator
var sameCollection = collection == tempCollection; // false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM