String.Length Property

public int Length { get; }

Gets the number of characters in the current String object.

📄

Property Value

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

Remarks

The Length property returns the number of Char objects in the string. Note that in UTF-16 encoding, one or more characters may be represented by more than one Char object.

The value of the Length property is the number of elements in the string's internal array. For example, if a string contains "Hello", its Length is 5.

💻

Examples

using System; public class Example { public static void Main() { string str = "Hello, World!"; Console.WriteLine($"The string is: '{str}'"); Console.WriteLine($"The length of the string is: {str.Length}"); // Output: 13 string emptyString = ""; Console.WriteLine($"The length of the empty string is: {emptyString.Length}"); // Output: 0 string whiteSpaceString = " "; Console.WriteLine($"The length of the whitespace string is: {whiteSpaceString.Length}"); // Output: 3 } }
⚠️

Exceptions

This property does not throw any exceptions.

Note: This is a conceptual representation of MSDN documentation. Actual documentation may vary.