+ 3

Are Strings mutable in C#?

25th Mar 2017, 9:31 AM
Kassem Shehady
Kassem Shehady - avatar
1 Odpowiedź
+ 5
Mutable and immutable are English words meaning "can change" and "cannot change" respectively. The meaning of the words is the same in the IT context; i.e. -- a mutable string can be changed, and -- an immutable string cannot be changed. The meanings of these words are the same in C# / .NET as in other programming languages / environments, though (obviously) the names of the types may differ, as may other details. For the record: -- String is the standard C# / .Net immutable string type -- StringBuilder is the standard C# / .Net mutable string type To "effect a change" on a string represented as a C# String, you actually create a new String object. The original String is not changed ... because it is unchangeable. In most cases it is better to use String because it is easier reason about them; e.g. you don't need to consider the possibility that some other thread might "change my string". However, when you need to construct or modify a string using a sequence of operations, it may be more efficient to use a StringBuilder. http://stackoverflow.com/a/4274260/994298
25th Mar 2017, 9:33 AM
Kassem Shehady
Kassem Shehady - avatar