如果string是引用类型,如何将其值从另一个对象更改为数组 [英] If string is a reference type, how can change its value from another object as array
本文介绍了如果string是引用类型,如何将其值从另一个对象更改为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string是一个引用类型,如何将其值从另一个对象更改为数组,例如
数组:
string is a reference type,How can I change its value from another object as array for example
Array:
var numbers = new int[] {1,2,3};
var numbers2 = numbers;
numbers2[0] = 9;
Console.WriteLine(numbers[0]); //Output is:9 because arrays is a rference type
我尝试过:
字符串:
What I have tried:
String:
var numbers = new string(new char[]{'o','n','e'});
var numbers2 = numbers;
numbers2 = "two";
Console.WriteLine(numbers); //why the output here is "one" it should be "two" it is a reference type??!!
推荐答案
因为...字符串是引用类型! :笑:
看看这个开头:使用struct和class - 那是什么一切? [ ^ ]它解释了差异,以及分配引用时发生了什么。 (你可能想暂时跳过下半场,对初学者来说有点忙。)
一旦你有一个好的阅读,它将有希望感觉,然后你可以准确地解释你想要做什么,我会试着解释如何做。
Because...string is a reference type! :laugh:
Have a look at the start of this: Using struct and class - what's that all about?[^] it explains teh difference, and what is going on when you assign references. (You might want to skip the last half for the moment, it gets a bit busy for beginners).
Once you've had a good read it will hopefully make sense, and then you can explain exactly what you are trying to do and I'll try to explain how to do it.
这篇关于如果string是引用类型,如何将其值从另一个对象更改为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文