字符串和类,当两者都是引用类型 [英] Strings vs classes when both are reference types

查看:125
本文介绍了字符串和类,当两者都是引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上次面试的方式:



问题:字符串存储在哪里?



回答:由于它是引用类型,因此堆。



请解释下面的代码:

  static void Main(string [] args)
{
string one =test;
string two = one;
one = one +string;

Console.WriteLine(One is {0},one);
Console.WriteLine(Two is {0},two);
}

回答: / p>

(代表语句 string two = one;





one = one +string; 。在堆上创建一个新的字符串并分配)



img src =https://i.stack.imgur.com/H7qlX.pngalt =enter image description here>



问题: / strong>正确。对于下面的代码片段绘制类似的代码:

  class Program 
{
static void Main(string [] args)
{
测试一个= new Test {someString =test};
测试两个=一个
one.someString =test String ;

Console.WriteLine(One is {0},one.someString);
Console.WriteLine(Two is {0},two.someString);
}
}

class Test
{
public string someString {get;组; }
}

回答:
$ b

[测试两=一]

  one.someString =test String 



问题:确定。你说两个 strings classes 是引用类型。那么为什么会创建一个新的字符串对象并赋值,而对于类,现有的字符串属性本身会被修改?



因为字符串是不可变的,而类是可变的。



(虽然我清除了面试,但是我还是不明白这个行为,为什么类的设计器string

解决方案

div>

其中一个原因是字符串是不可变的,即使它们是引用类型,是使它们看起来和行为像原始类型(例如,int,double,float)。



这也是为什么字符串是唯一可以表示为文字(例如,一些字符串)的引用类型的原因。许多其他语言采用相同的方法,例如Java。


Here is how my last interview went:

Question: Where are strings stored?

Answer: Heap since it is a reference type

Question: Explain me this following code:

static void Main(string[] args)
{
     string one = "test";
     string two = one;
     one = one + " string";

     Console.WriteLine("One is {0}" , one);
     Console.WriteLine("Two is {0}", two);
}

Answer: Drew two diagrams like below:

(represents the statement, string two = one;

(represents the statement, one = one + " string";. A new string is created on heap and assigned)

Question: Correct. Draw similar for the code snippet below:

class Program
{
    static void Main(string[] args)
    {
        Test one = new Test { someString = "test" };
        Test two = one;
        one.someString = "test String";

        Console.WriteLine("One is {0}", one.someString);
        Console.WriteLine("Two is {0}", two.someString);
    }
}

class Test
{
    public string someString { get; set; }
}

Answer:

[Test two = one]

one.someString = "test String";

Question: Ok. You said both strings and classes are reference types. Then why did a new object for string is created and assigned the value whereas for class, the existing string property itself gets modified ?

Answer: Because strings are immutable whereas classes are mutable.

(Though I cleared the interview, I still did not understand this behaviour. Why did the designers of class make it mutable while keeping strings immutable ? There are a lot of therotical answers everywhere, but could any one make it simple by explaining this particular behaviour with the above code ?)

解决方案

One of reasons strings were made immutable, even though they are reference types, was to make them look and behave like primitive types (e.g., int, double, float).

That's also the reason why strings are the only reference type that can be represented as a literal (e.g., "some string"). Lots of other languages take the same approach, like Java for example.

这篇关于字符串和类,当两者都是引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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