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 Boh';
Is length being computed now?
OR
Is it computed only after I try to get A.Length?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Firstly, note that strings in .NET are very different to strings stored in unmanaged languages (such as C++)…
System.Stringtype is immutable.Because of the way memory usage works in the CLR, you can essentially consider that getting the
Lengthproperty of a string is just like retrieving anintvariable. The performance cost here is going to be absolutely minimal, if that’s what you’re considering.If you want to read up more about strings in .NET, try Jon Skeet’s article on the topic – it seems to have all the details you might ever want to know about strings in .NET.