在C#字符串赋值 [英] String assignment in C#

查看:159
本文介绍了在C#字符串赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个星期前,我发现在C#中的字符串被定义为引用类型而不是值类型。最初,我糊涂了这一点,但随后一些阅读后,我突然明白了为什么它是在堆上,而不是堆栈存储字符串重要 - 因为这将是非常低效的有得到了一个不可预知的号码复制一个非常大的字符串的堆栈帧。我完全接受这个



我觉得我的理解是几乎完成,但,我很想念一个元素 - 做字符串使用什么语言功能,让他们一成不变?要使用代码示例说明:

 字符串值a =FirstValue; 
串valueB,则=值a;
值a =AnotherValue;

Assert.AreEqual(FirstValuevalueB,则); //传递



我不明白的语言特点使得值a的副本,当我把它分配给valueB,则。或许,在参考值a,当我把它分配给valueB,则不会改变,只有值a得到一个新的参考给自己当我设置字符串。由于这是一个实例类型,我不明白为什么这个工程。



据我了解,你可以重载,例如,==和!=运营商,但我似乎无法找到超载=运营商的任何文件。是什么原因呢?


解决方案

做字符串使用什么语言功能,让他们一成不变?




这是不是一种语言功能。它是类的定义方式。



例如,

  Integer类{
私人只读int值;

公共int值{{返回THIS.VALUE; }}
公共整型(int值){THIS.VALUE =价值; }}
公共整数添加(整数等){
返回新的整数(THIS.VALUE + other.value);
}
}



就像是一个 INT ,除了它是一个引用类型,但它是不可变的。我们的定义的它是如此。我们可以将其定义为是可变的,太:

 类MutableInteger {
私人int值;

公共int值{{返回THIS.VALUE; }}
公共MutableInteger(int值){THIS.VALUE =价值; }}
公共MutableInteger添加(MutableInteger等)​​{
THIS.VALUE = THIS.VALUE + other.value;
返回这一点;
}
}

请参阅?




我不明白的语言特点使得副本值a 当我把它分配给 valueB,则




它不会复制字符串,它复制引用。 字符串是引用类型。这意味着类型的变量字符串是存储位置,其值引用。在这种情况下,它们的值是字符串的实例的引用。当您指定类型的变量字符串另一种类型的字符串,该值将被复制。在这种情况下,该值是一个参考,它是由分配复​​制。这是对任何引用类型,而不仅仅是字符串或仅不变引用类型正确的。




也许,提到值a 当我把它分配给不改变 valueB,则,只值a 获取一个新的参考到自身时,我设定的字符串。




不,值a的值 valueB,则参考字符串的同一个实例。它们的值是引用,这些值是相等的。如果你能以某种方式发生变异 * 字符串的实例,通过值a 称,该referrent双方值a valueB,则将看到这种突变。



<块引用>

由于这是一个实例类型,我不明白为什么这个工程。




有没有这样的事,作为一个实例类型。



基本上,字符串是引用类型。但字符串是不可改变的。当变异一个字符串,什么情况是,你到一个新的字符串,它是突变的现有字符串的结果的参考

 字符串s =你好,世界!; 
串T = S;
串U = s.ToUpper();



在这里,取值 T 是变量,其值是指字符串的同一个实例 S的referrent 不被调用 String.ToUpper 突变。相反, s.ToUpper 使的referrent 取值的突变,并返回到<$的一个新实例的引用C $ C>字符串,它创建了应用突变的过程。我们分配一个参照 U




据我了解,你可以重载,为例如,==和!=操作符,但我似乎无法找到超载=运营商的任何文档。




您可以 ŧ超载 =



* 可以,有一些技巧。忽略它们。


A few weeks ago, I discovered that strings in C# are defined as reference types and not value types. Initially I was confused about this, but then after some reading, I suddenly understood why it is important to store strings on the heap and not the stack - because it would be very inefficient to have a very large string that gets copied over an unpredictable number of stack frames. I completely accept this.

I feel that my understanding is almost complete, but there is one element that I am missing - what language feature do strings use to keep them immutable? To illustrate with a code example:

string valueA = "FirstValue";
string valueB = valueA;
valueA = "AnotherValue";

Assert.AreEqual("FirstValue", valueB); // Passes

I do not understand what language feature makes a copy of valueA when I assign it to valueB. Or perhaps, the reference to valueA does not change when I assign it to valueB, only valueA gets a new reference to itself when I set the string. As this is an instance type, I do not understand why this works.

I understand that you can overload, for example, the == and != operators, but I cannot seem to find any documentation on overloading the = operators. What is the explanation?

解决方案

what language feature do strings use to keep them immutable?

It is not a language feature. It is the way the class is defined.

For example,

class Integer {
    private readonly int value;

    public int Value { get { return this.value; } }
    public Integer(int value) { this.value = value; } }
    public Integer Add(Integer other) {
        return new Integer(this.value + other.value);
    }
}

is like an int except it's a reference type, but it's immutable. We defined it to be so. We can define it to be mutable too:

class MutableInteger {
    private int value;

    public int Value { get { return this.value; } }
    public MutableInteger(int value) { this.value = value; } }
    public MutableInteger Add(MutableInteger other) {
        this.value = this.value + other.value;
        return this;
    } 
}

See?

I do not understand what language feature makes a copy of valueA when I assign it to valueB.

It doesn't copy the string, it copies the reference. strings are reference type. This means that variables of type strings are storage locations whose values are references. In this case, their values are references to instances of string. When you assign a variable of type string to another of type string, the value is copied. In this case, the value is a reference and it is copied by the assignment. This is true for any reference type, not just string or only immutable reference types.

Or perhaps, the reference to valueA does not change when I assign it to valueB, only valueA gets a new reference to itself when i set the string.

Nope, the values of valueA and valueB refer to the same instance of string. Their values are references, and those values are equal. If you could somehow mutate* the instance of string referred to by valueA, the referrent of both valueA and valueB would see this mutation.

As this is an instance type, I do not understand why this works.

There is no such thing as an instance type.

Basically, strings are reference types. But string are immutable. When you mutate a string, what happens is that you get a reference to a new string that is the result of the mutation to the already existing string.

string s = "hello, world!";
string t = s;
string u = s.ToUpper();

Here, s and t are variables whose values refer to the same instance of string. The referrent of s is not mutated by the call to String.ToUpper. Instead, s.ToUpper makes a mutation of the referrent of s and returns a reference to a new instance of string that it creates in the process of apply the mutation. We assign that reference to u.

I understand that you can overload, for example, the == and != operators, but I cannot seem to find any documentation on overloading the = operators.

You can't overload =.

* You can, with some tricks. Ignore them.

这篇关于在C#字符串赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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