Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will happen when we access the ' Length ' property of string in C#?

Tags:

string

c#

The sample code like this:

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 of string will be accessed more than once, is the first snippet code better than the second? Why?

When we access the length of string in the way of hi.Length, the CLR will count the number of character in hi and return it or just return a 10 cause the Length property has been assigned a value of 10 at the time hi is initialized or something else?

How about in Java?

like image 402
Wenbo Avatar asked Jan 22 '16 09:01

Wenbo


People also ask

Which property is used to get length of the string?

The length property returns the length of a string. The length property of an empty string is 0.

Which property can be used to find the length of a string C#?

Use the String. Length property in C# to get the length of the string.

How do you find the length of a string?

To calculate the length of a string in Java, you can use an inbuilt length() method of the Java string class. In Java, strings are objects created using the string class and the length() method is a public member method of this class. So, any variable of type string can access this method using the . (dot) operator.

How does the length property of a string work?

The Length property of a string is the number of Char objects it contains, not the number of Unicode characters. The length property returns the number of Char objects instead of the number of Unicode characters because a Unicode character might be represented by more than one character.

How to find the length of a string in C?

String length in C. C program to find length of a string, for example, the length of the string "C programming" is 13 (space character is counted). The null character isn't counted when calculating it. To find it, we can use strlen function of "string.h." C program to find length of a string without using strlen function, recursion.

How to find length of a string without using strlen function?

To find length of a string, we can use strlen function of "string.h.". C program to find length of a string without using strlen function, recursion.

How does the function lengthofstring call itself?

The Function LengthofString calls itself until the character of string is’nt a null character it calls itself, when it calls itself it increases the value of the variable ‘n’ which stores number of times the function has been called and when it encounters the null character the function prints the value of ‘n’ and returns back in the same direct...


Video Answer


1 Answers

Length is a property of an array (string is an array of characters internally) which means it's always already available in memory (an array is always fixed length so the Length property won't change). In both cases you should always just use hi.Length instead of declaring another variable. Now you are just saving the length twice in memory (once on the stack for the int and once on the heap for the property).

Edit: As commenters below have pointed out, this method is optimised for ICollection to use the property instead of iterating. If you were to use the linq method .Count(), this would cause your program to iterate over the entire array to count the elements.

like image 51
Alexander Derck Avatar answered Oct 21 '22 04:10

Alexander Derck