System.String.Length Property

Gets the number of characters in the current string object.

Syntax

public int Length { get; }

Value

A int containing the number of characters in the current instance.

Remarks

The string.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 string.Length property returns 7, which indicates that it includes the six alphabetic characters as well as the null character.

code reference: System.String.Class#1

Example

The following example demonstrates the string.Length property.

C# Example

using System;
public class StringLengthExample {
 public static void Main() {
 string str = "STRING";
 Console.WriteLine( "The length of string {0} is {1}", str, str.Length );
 }
}

The output is

The length of string STRING is 6

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0