Just got this confusion running in my mind throughout the day. I am very much confused between reference and value type being passed to a method.
Say I have 2 classes
Beside Reference Type and Value Type, there are Mutable Type and Immutable Type.
Immutable means that object cannot and will not be changed after initialization. As a result, your statement only produces new string but does not modify original string.
s += "Hi";
The hello string object remains hello. Change is that s is assigned with a new object helloHi.
You are unfortunate enough using string as an example.
Try to use mutable types like StringBuilder in your example.
public class C
{
public static void Main(string[] args)
{
StringBuilder s = new StringBuilder("hello");
StringBuilder w = Changestring(s);
StringBuilder x = s;
}
private static StringBuilder Changestring(StringBuilder s)
{
s.Append("Hi");
return s;
}
}