8 out of 14 rated this helpful - Rate this topic

String.Length Property

Gets the number of characters in the current String object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public int Length { get; }

Property Value

Type: System.Int32
The number of characters in the current string.

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. Use the System.Globalization.StringInfo class to work with each Unicode character instead of each Char.

In some languages, such as C and C++, a null character indicates the end of a string. In the .NET Framework, a null character can be embedded in a string. When a string includes one or more null characters, they are included in the length of the total string. For example, in the following string, the substrings "abc" and "def" are separated by a null character. The Length property returns 7, which indicates that it includes the six alphabetic characters as well as the null character.


StringBuilder charactersBuilder = new StringBuilder("abc");
string characters;

charactersBuilder.Append('\0');
charactersBuilder.Append("def");

characters = charactersBuilder.ToString();
Console.WriteLine(characters.Length);


The following example demonstrates the Length property.


// Sample for String.Length
using System;

class Sample 
{
    public static void Main() 
    {
    string str = "abcdefg";
    Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length);
    Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length);
    }
}
/*
This example produces the following results:
1) The length of 'abcdefg' is 7
2) The length of 'xyz' is 3
*/


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
string may not be null

You neglected to mention that string may not be null, or an exception will be thrown.

Why Is the NullReferenceException Not Documented?

Calling a method on a reference type whose value is null always throws an exception. This is standard not only in the .NET Framework, but in other frameworks and in the executables emitted by other language compilers as well. So, for example, the following code will always throw a NullReferenceException:

string s = null;
int l = s.Length;


Because you should always ensure that your reference types have valid references, and because their members always throw a NullReferenceException if they are called on a null reference, that exception is not documented for any of the members of types belonging to the .NET Framework.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation