Even though "String" is a reference type, in VB.NET, we only get the effect of using of a reference type if we pass a parameter as ByRef. So, unlike C# even reference types in VB.NET behaves like value type by default. Why is there this kind of difference?
-
1`String`s are implemented differently: http://blogs.msdn.com/b/ericlippert/archive/2011/07/19/strings-immutability-and-persistence.aspx – Leri Jul 26 '13 at 08:20
-
http://msdn.microsoft.com/en-us/library/vstudio/362314fe.aspx – Philip Gullick Jul 26 '13 at 08:21
-
2What you state is not true. Strings are immutable. They are still reference types, but since you cannot modify the content of the string after it has been created, you cannot mutate it. There's no relevant different between C# and VB.net in this area. Perhaps it would be better if you showed your code that led you to the conclusion that you state in the question. Then we'd have more context. – David Heffernan Jul 26 '13 at 08:21
-
also see: http://stackoverflow.com/questions/636932/in-c-why-is-string-a-reference-type-that-behaves-like-a-value-type and http://stackoverflow.com/questions/1069155/is-string-a-value-type-or-a-reference-type – Leon Jul 26 '13 at 08:23
2 Answers
If you are trying to understand reference types and value types and their differences in VB.NET and C#.NET using string as an example then you will confuse yourself big time.
Just as David mentioned, strings are reference types, but they are special, that is, immutable. Which means that once you have created a string on a reference address you cannot modify them. If you try to do that then .NET will store your modified string on a different address and start pointing to the new address. The old address will get garbage collected over a period of time.
For example
string str = "new string"; //One address space
str = "modified" + str; //Different address and not same address as above
Moreover, when you are passing a string by reference to a method and modifying the string in the method, it is actually just pointing to a different reference, but it makes you feel that the actual reference is updated.
I hope this clarifies your question a little.

- 30,738
- 21
- 105
- 131

- 5,021
- 9
- 47
- 71
-
This is slightly misleading – strings are *not* special. It’s just any other reference type. *Any* reference type can be written so that it behaves immutably. You probably meant that but it’s worth stressing because it’s not something most beginners understand readily. – Konrad Rudolph Jul 26 '13 at 13:18
-
Yes that is what I meant but did not mention "Any reference type can be written so that it behaves immutably" only to avoid confusion. Also I mentioned "special" because, of all the types given by .net, only string is a type which is immutable (I think I am correct in making this statement). Yes of course a developer can create a custom immutable type if he/she wants. Anyways thanks for pointing it out. :) – samar Jul 26 '13 at 13:46
No, System.String is as much a reference type in C# as it is in VB.NET. It is however a bit special; it doesn't have any method or property that lets you change the string.
You can only ever assign a string reference variable with another string object. It tends to confuse programmers because the syntax resembles the way you assign a value type value; you very rarely use the New operator. You don't have to; assigning a literal doesn't require New. And System.String has many methods that return a new string object; creating the object is done inside the method. Notable is that using the Replace() method and forgetting to use the return value is a very common mistake.
This design was quite intentional; it makes it safe to pass a string as a method argument without any risk that the called method will change it. And automatically makes a string thread-safe. Both very important properties for such a common type.
It does have a notable disadvantage; your program tends to generate a lot of garbage when it uses strings. Since they rarely live for very long. Which is okay; the garbage collector was written to optimize this case, and it performs generation 0 collections very quickly. The StringBuilder class is a fallback; it is mutable.

- 922,412
- 146
- 1,693
- 2,536