What happens when we access the "Length" property of a string in C #?

Sample code as follows:

string hi = "HelloWorld"; int length = hi.Length; Console.WriteLine(length); Console.WriteLine(length); ... string hi = "HelloWorld"; int length = hi.Length; Console.WriteLine(hi.Length); Console.WriteLine(hi.Length); ... 

If the Length property for a string is available more than once, is the first code of the fragment better than the second? Why?

When we access the length of the string in the form of hi.Length , will the CLR count the number of characters in hi and return it or just return 10 does the Length property assign value 10 at the time of hi initialization or something else?

How is it in Java?

+5
source share
3 answers

Your question is what is going on in .NET at a really low level. What really happens when running JIT and optimized code may be different than expected.

However, the way to answer your question is to look at the generated IL:

 string hi = "HelloWorld"; int length = hi.Length; Console.WriteLine(length); Console.WriteLine(length); 

compiles to

 ldstr "HelloWorld" callvirt System.String.get_Length dup call System.Console.WriteLine call System.Console.WriteLine 

and

 string hi = "HelloWorld"; Console.WriteLine(hi.Length); Console.WriteLine(hi.Length); 

(where I removed the length assignment because it is not used) compiles to

 ldstr "HelloWorld" dup callvirt System.String.get_Length call System.Console.WriteLine callvirt System.String.get_Length call System.Console.WriteLine 

The first version looks a bit more efficient than the second, because there is only one call to System.String.get_Length , and both use an extra stack location ( dup ). However, JIT'er could possibly have built in this call, which would then use indirection to read the value from the memory location, and then there is hardly any difference.

Note that the .NET string type stores the length of the string in the string object, so there is no need to count the characters in the string. The length is known when creating a string.

+4
source

Length is an array property (a string is an array of characters inside), which means that it is always available in memory (the array is always fixed, so the Length property will not change). In both cases, you should always just use hi.Length instead of declaring another variable. Now you just save the length twice in memory (once on the stack for int and once on the heap for property).

Edit: As commentators note below, this method is optimized for ICollection to use property instead of iteration. If you want to use the linq .Count() method, this will cause your program to iterate over the entire array to count the elements.

+11
source

According to MSDN, String.Length is a property that will give you the number of characters in the current String object.

And in your case, the second option is relatively better; as it can avoid extra int variable . And also String.Length will not create any instance or additional memory space, it will return the allocated memory space on the heap for the string.

0
source

Source: https://habr.com/ru/post/1241205/


All Articles