How Does Knowing Ref and Val types Help
I have been reading up on Reference and value types. I get the idea. But how does this help me while coding? I don't have any good examples to understand those times where knowing this stuff helps.
It means you can understand what your code is going to do.
It's kinda hard to write code when you can't predict what the effects of any given statement will be, due to not knowing the type system semantics.
For example, suppose you don't know how reference types work, and someone presented you with this code:
StringBuilder builder = new StringBuilder(); StringBuilder other = builder; builder.Append("Foo"); other.Append("Bar"); Console.WriteLine(builder);
We know that that will print "FooBar" because StringBuilder is a reference type - the second line is only copying a reference, not creating another object. But if you didn't understand how reference types behave, you might have expected it just to print "Foo".