Is string.Length in C# (.NET) instant variable?

前端 未结 4 1094
陌清茗
陌清茗 2020-12-11 16:24

I\'m wondering if string.Length in C# is an instant variable. By instant variable I mean, when I create the string:

string A = \"\";

A = \"Som          


        
4条回答
  •  醉梦人生
    2020-12-11 16:42

    .NET strings are stored with the length pre-computed and stored at the start of the internal structure, so the .Length property simply fetches that value, making it an O(1) function.

提交回复
热议问题