What is immutability and why should I worry about it?

前端 未结 15 1082
无人共我
无人共我 2020-12-07 09:51

I\'ve read a couple of articles on immutability but still don\'t follow the concept very well.

I made a thread on here recently which mentioned immutability, but as

15条回答
  •  甜味超标
    2020-12-07 10:47

    "... why should I worry about it?"

    A practical example is repetitive concatenation of strings. In .NET for example:

    string SlowStringAppend(string [] files)
    {
        // Declare an string
        string result="";
    
        for (int i=0;i

    Unfortunately using the first (slow) function approach is still commonly used. An understanding of immutability makes it very clear why using StringBuilder is so important.

提交回复
热议问题