Even if you know Object Oriented Programming in certain languages like Java, learning C# is totally different. One of the features that varies greatly in C# from Java is passing values by reference as parameters of methods. In Java, data types are automatically passed by value while classes are passed by reference. Good thing I decided to read out a beginner’s book for C#.
In C#, if you want to pass objects by reference, you would have to use the keyword ref. Take for example the sample code below
|
1 2 3 4 5 |
string str = "haha"; public void change(string param) { param = "hoho"; } |
If you would call the method change() like this
|
1 2 |
change(str); Console.WriteLine(str); |
the output for the object str would be haha instead of hoho. You would have to use the ref keyword for this to work
|
1 2 3 |
public void change(ref string param) { param = "hoho"; } |
Data types are a different story. To do the same way to data types as parameters in methods, you would use the out keyword like this
|
1 2 3 4 5 |
int i = 1; public void change(out int param)_ { param = 10; } |
Calling the change method
|
1 2 |
change(i); Console.WriteLine(i); |
will output 10 as the value. If you do not use the out keyword, the value of i will still be 1.

I totally forgot about all of these. Are you constantly using this language?
My Daily Thoughts
Money Journal
Blogger”s Recollections
Thanks for visiting my other blogs.
Celebrity Entertainment
Array Of Hopes
In this case, actually the string is the mutable class, once it is initialized it cannot be modified. And I found some useful link here: Here.
Class is always send by reference. But the use of ref will be the special case in some coding style.
What’s others say?
I think you should see this
Link
Thanks