We have Object and ValueType…. And String….
String is derived from object, but it is immutable. Is it considered a type of it’s own as it doesn’t behave like other objects? Is it the only object that has this immutable behaviour? Is it the runtime, compiler or library that defines this? And are there other cases like this in .Net?
Edit: Yes, I too create classes that isn’t allowed to change after constructed; immutable objects. But isn’t string more special than this?
stringis a reference type. There are plenty of other immutable types though, and you can create your own: just don’t provide any members which change the state! Here’s an example:Of course,
stringalso overloads == and !=, overridesEqualsandGetHashCodeetc… all of which can be done in your own types too.stringdoes have some genuinely special properties though:new String(new char[0])repeatedly, you’ll get the same reference every time