C#按引用赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9792776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# assign by reference
提问by Marin
Is it possible to assign by reference? I know that ref has to be used in methods.
是否可以通过引用分配?我知道必须在方法中使用 ref 。
string A = "abc";
string B = A;
B = "abcd";
Console.WriteLine(A); // abc
Console.WriteLine(B); // abcd
Can I have some sort of
我可以有某种
string A = "abc";
string B = (ref)A;
B = "abcd"; // A was assigned to B as reference, so changing B is the same as changing A
Console.WriteLine(A); // abcd
Console.WriteLine(B); // abcd
采纳答案by kemiller2002
You aren't modifying the reference to A. You are creating a whole new string. A still shows "abc", because it can't be changed by modifying B. Once you modify B, it points to a whole new object. Strings are immutable too, so any change to one creates a new string.
您不是在修改对 A 的引用。您是在创建一个全新的字符串。A仍然显示“abc”,因为它不能通过修改B来改变。一旦你修改了B,它就指向一个全新的对象。字符串也是不可变的,因此对一个字符串的任何更改都会创建一个新字符串。
To further answer your question with non-immutable reference types, it is possible to modify the properties of an object that a variable points to and it will show the changed effect when you access other variables pointing to the same object. This does not mean however that you can have a variable point to a brand new object, and have other variables (that pointed to the old object) point to that new object automatically without modifying them as well.
要使用非不可变引用类型进一步回答您的问题,可以修改变量指向的对象的属性,当您访问指向同一对象的其他变量时,它将显示更改后的效果。然而,这并不意味着您可以让一个变量指向一个全新的对象,并且让其他变量(指向旧对象)自动指向该新对象,而无需修改它们。
回答by Chris Shain
That's how it works already. Strings are a reference type- your variable A is a reference (like a pointer) to a string on the heap, and you are just copying the pointer's value (the address of the string) into the variable B.
这就是它的工作方式。字符串是一种引用类型——你的变量 A 是对堆上字符串的引用(就像一个指针),而你只是将指针的值(字符串的地址)复制到变量 B 中。
Your example doesn't change the value of A when you assign "abcd" to B because strings are treated specially in .net. They are immutable, as Kevin points out- but it is also important to note that they have value type semantics, that is assignments always result in the reference pointing to a new string, and doesn't change the value of the existing string stored in the variable.
当您将“abcd”分配给 B 时,您的示例不会更改 A 的值,因为在 .net 中对字符串进行了特殊处理。它们是不可变的,正如 Kevin 指出的那样 - 但同样重要的是要注意它们具有值类型语义,即赋值总是导致指向新字符串的引用,并且不会更改存储在的现有字符串的值变量。
If, instead of Strings, you used (for example) cars, and changed a property, you'd see this is the case:
如果,而不是字符串,您使用(例如)汽车,并更改了属性,您会看到这种情况:
public class Car {
public String Color { get; set; }
}
Car A = new Car { Color = "Red" };
Car B = A;
B.Color = "Blue";
Console.WriteLine(A.Color); // Prints "Blue"
// What you are doing with the strings in your example is the equivalent of:
Car C = A;
C = new Car { Color = "Black" };
It's probably worth noting that it does not work this way for value types (integers, doubles, floats, longs, decimals, booleans, structs, etc). Those are copied by value, unless they are boxed as an Object.
可能值得注意的是,它不适用于值类型(整数、双精度、浮点数、长整数、小数、布尔值、结构体等)。这些是按值复制的,除非它们被装箱为Object.
回答by joshperry
Strings are already references, after B = A then B.equals(A) will return true. However, when you do B = "abcd" you're doing the same thing, you're assigning B to a reference to the string literal.
字符串已经是引用,在 B = A 之后 B.equals(A) 将返回 true。但是,当您执行 B = "abcd" 时,您正在做同样的事情,您将 B 分配给对字符串文字的引用。
What you are wanting to do is modify the data pointed to by the string, however, because Strings in .NET are immutable there is no way to do that.
您想要做的是修改字符串指向的数据,但是,因为 .NET 中的字符串是不可变的,因此无法做到这一点。
回答by Joel Coehoorn
public class ReferenceContainer<T>
{
public T Value {get;set;}
public ReferenceContainer(T item)
{
Value = item;
}
public override string ToString()
{
return Value.ToString();
}
public static implicit operator T (ReferenceContainer<T> item)
{
return Value;
}
}
var A = new ReferenceContainer<string>("X");
var B = A;
B.Value = "Y";
Console.WriteLine(A);// ----> Y
Console.WriteLine(B);// ----> Y
回答by the_joric
Strings are immutable that's true. However you can resolve your issue by encapsulating string within a class and making A and B instances of that class. Then A = B should work.
字符串是不可变的,这是真的。但是,您可以通过将字符串封装在一个类中并创建该类的 A 和 B 实例来解决您的问题。那么 A = B 应该可以工作。
回答by Raj Ranjhan
Strings are special objects in C# because they are immutable, otherwise it would be by reference. You can run this snippet to see.
字符串是 C# 中的特殊对象,因为它们是不可变的,否则它将通过引用。您可以运行此代码段来查看。
public class Foo
{
public string strA;
}
Foo A = new Foo() { strA = "abc" };
Foo B = A;
B.strA = "abcd";
Console.WriteLine(A.strA);// abcd
Console.WriteLine(B.strA);//abcd

