I was learning C# and when I got to the section of Properties, I learned about the one-liner format (ex. public int Property { get; set; }
).
After that, I got curious about the String class's Length property and its implementation. When I look at the documentation, it simply reads
public int Length { get; }
even though I would expect a for loop within get
to count the characters.
From what I was able to gather, you don't always need a separate member variable (in this case: private int length
) when defining a property, but I might expect some internal process counting the characters, storing them in length
and then returning that value when get
is called from myString.Length
. Is there some lower level process occurring when the length of the string is counted or am I not seeing something obvious?