I'm using them alternately, is there any difference between them?
The length or size of a string means the total number of characters present in it.
Length is a property returning the number of elements in an Array . Count() is a LINQ extension that does the same on an IEnumerable . Optionally, it can take a predicate as parameter, and will return the number of elements that satisfy it.
As you know, the best way to find the length of a string is by using the strlen() function.
The string length method returns the number of characters written in the String. This method returns the length of any string which is equal to the number of 16-bit Unicode characters in the string.
On the surface they would seem functionally identical, but the main difference is:
Length is a property that is defined of strings and is the usual way to find the length of a string
.Count() is implemented as an extension method. That is, what string.Count() really does is call Enumerable.Count(this IEnumerable<char>), a System.Linq extension method, given that string is really a sequence of chars.
Performance concerns of LINQ enumerable methods notwithstanding, use Length instead, as it's built right into strings.
String implements the IEnumerable, so it has a method Count while Length is a property in the String class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With