Why strings behave like ValueType

后端 未结 7 1999
感动是毒
感动是毒 2020-12-16 23:44

I was perplexed after executing this piece of code, where strings seems to behave as if they are value types. I am wondering whether the assignment operator is operating on

7条回答
  •  攒了一身酷
    2020-12-17 00:16

    There are three semantic types of entities:

    1. Mutable reference types
    2. Mutable value types(*)
    3. Immutable types

    If one makes a copy X of a mutable reference Y and then does something with the copy, any mutation performed upon X will affect Y, and vice versa, since X and Y both refer to the same object. By contrast, if one makes a copy XX of a mutable value type instance YY, changes to XX will not affect YY, nor vice versa.

    Because the only semantic difference between reference types and value types is the behavior if they are altered after they are copied, immutable reference types are semantically identical to immutable value types. That is not to imply that there aren't sometimes considerable performance advantages to using one over the other.

    (*) Meaning value types which can be partially altered without being completely replaced. Point, for example, is mutable because one can change part of it without having to read and rewrite the whole thing. By contrast, Int32 is immutable, since (at least from "safe" code) it's not possible to make any change to an Int32 without rewriting the whole thing.

提交回复
热议问题