
Every string object has a length. The C# language accesses the Length property on the string type. Every character—no matter what it is—is counted in the Length property. Length is cached on the string type. It is possible to avoid exceptions while accessing Length.
This C# article shows how to use the Length property on the string type.
Key point: Length returns the character count in the string instance. The string cannot be null.

First, string is a reference type that has a Length property, which returns the number of characters in its internal buffer. You cannot iterate through every character of a string, checking for '\0'. Here we see four examples of using the Length property on strings. The four strings we use are all of different Lengths, and there are variable strings, constant strings, and literal strings.
Program that uses Length [C#]
using System;
class Program
{
static void Main()
{
// An example string
string a = "One example";
Console.WriteLine(a.Length);
// An empty example string
string b = "";
Console.WriteLine(b.Length);
// A constant string
const string c = "Three";
Console.WriteLine(c.Length);
// A literal string
Console.WriteLine("Four".Length);
}
}
Output
11
0
5
4Description. The final part above shows how you can treat a string literal, contained in quotation marks, as an object in the C# language. This is a powerful feature and can help make your code clearer by avoiding magic constants.
String Literal
To continue, we see that you must first test for null strings before calling the Length property. This is because you cannot access members on null reference types. The IsNullOrEmpty method is ideal for this. Additionally, there are some interesting behaviors with null strings at the class level.
Null String UsageProgram that deals with null Length [C#]
using System;
class Program
{
static void Main()
{
F(null);
F("cat");
F("book");
F("");
}
static void F(string a)
{
if (string.IsNullOrEmpty(a))
{
// String is null or empty
Console.WriteLine(true);
}
else
{
// Print length of string
Console.WriteLine(a.Length);
}
}
}
Output
True
3
4
True
Microsoft's article on this subject has some interesting details on string Length. It tells you that the Length is not derived from the number of chars, but not symbolic Unicode chars.
MSDN referenceMSDN: "The Length property returns the number of Char objects in this instance, not the number of Unicode characters. The reason is that a Unicode character might be represented by more than one Char."
Note. It is possible for null characters to be in the middle of C# strings. This is not a common occurrence in purely managed code, however.

Here we explore the idea of caching string Length integers. If you are using a string thousands of times and accessing the Length property each time, this may be incurring some overhead.
When accessing string Length, this MSIL instruction will be generated: callvirt instance int32 [mscorlib]System.String::get_Length(). Next, the value will be retrieved from an internal part of the .NET runtime.
Benchmark. I experimented with storing, or hoisting, the string Length outside of a tight loop. My result was that avoiding recalculating Length can have a performance benefit. Please see the benchmark result chart below.
Input data in benchmark [C#]
// Example string
string one = "one" + 1.ToString();
// Cached length
int cl = one.Length;
Code benchmarked in loops [C#]
1000000000 iterations tested.
// A
int len = one.Length;
if (one[len - 1] == 'x')
{
}
// B
if (one[cl - 1] == 'x')
{
}
Results
Access Length repeatedly: 1305 ms
Hoist Length out of loop: 646 ms [faster]
The string type in C# provides the Length property, which is useful for finding the number of chars in a string. We saw an example of the Length property being used on four different strings, as well as with null and empty strings. Finally we found that hoisting the string Length out of a tight loop can improve performance.
Property Examples String Type