Reference Vs Value types in C#
Strings are reference types and integers are value types. Some say that, its done because of location where they are stored. Value types are stored at Stack and Reference types at heap. So, essentially strings are big and will eat up all space, if stored in Stack.
So, as Eric mentioned in his blog, we should be considering value and reference types by looking at there semantics and not where they are stored. Value types are copied as value and reference types copy references.
This should be the only reason for deciding whether your data should be value type or reference types.
Though string is a reference type it behave as a value type because its immutable, but its seems its not internally, they just appear to be. Immutable means object whose state cannot be modified once created.
Strings are reference types and integers are value types. Some say that, its done because of location where they are stored. Value types are stored at Stack and Reference types at heap. So, essentially strings are big and will eat up all space, if stored in Stack.
So, as Eric mentioned in his blog, we should be considering value and reference types by looking at there semantics and not where they are stored. Value types are copied as value and reference types copy references.
This should be the only reason for deciding whether your data should be value type or reference types.
Though string is a reference type it behave as a value type because its immutable, but its seems its not internally, they just appear to be. Immutable means object whose state cannot be modified once created.
No comments:
Post a Comment