C# reference type | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

C# reference type

Hi! Explain me please next. Array and string in C# are reference type. But array i can change in method without ref pointer, but string can't change. Why? https://code.sololearn.com/coNxJU36PwdF/?ref=app

25th Jun 2019, 7:07 AM
id001x
id001x - avatar
2 Answers
+ 2
Quote : Even though strings are reference types, strMain isn't passed by reference. It's a reference type, but the reference is being passed by value. This is a tricky distinction, but it's a crucial one. Any time you pass a parameter without the ref keyword (not counting out parameters), you've passed something by value. https://stackoverflow.com/questions/10792603/how-are-strings-passed-in-net
25th Jun 2019, 8:11 PM
sneeze
sneeze - avatar
+ 2
I thought about this question for a long time, re-read the article several times, which you threw out and highlighted the following explanation: a string is an array of characters, like any array, it is a reference type, but when we try to overwrite a string, we cannot actually do it because the size of the array is determined only during initialization, and as a result, the line is not overwritten, and the memory is allocated space for a new line that already has another reference. Therefore, passing a string to a method without a reference pointer does not change the string. And if I understand correctly, when passing a string to a method using a pointer, the method automatically changes the received pointer to a new one, which indicates the location of the new line, but the old one, again, does not actually change.
26th Jun 2019, 7:57 PM
id001x
id001x - avatar