Char Struct

Represents a character as a UTF-16 code unit.

Table of Contents

Overview

The Char struct is a value type that represents a single UTF-16 code unit. It is used to store characters in .NET and provides various methods for character manipulation, comparison, and conversion.

A UTF-16 code unit is a 16-bit unsigned integer whose value corresponds to a Unicode character. .NET uses UTF-16 encoding for strings, meaning that each character in a string is represented by one or two Char values.

The Char struct is part of the System namespace.

Syntax

public struct Char : IComparable, IConvertible, IEquatable<char>

Remarks

The Char struct implements the IComparable, IConvertible, and IEquatable<char> interfaces, allowing for comparison and conversion operations.

You can create a Char value by enclosing a character in single quotes, for example: 'A', '%', '\u0041'.

The Char struct has a range from '\0' (U+0000) to '\uffff (U+FFFF).

Example: char myChar = 'X'; char unicodeChar = '\u0042'; // Represents the character 'B'

Members

Fields

Name Description
MaxValue Represents the maximum value of a Char. This field is a constant and equal to '\uffff.
MinValue Represents the minimum value of a Char. This field is a constant and equal to '\0.

Methods

Static Methods

Signature Description
bool IsControl(char c) Determines whether the specified Unicode character is a control character.
bool IsDigit(char c) Determines whether the specified Unicode character is a decimal digit.
bool IsLetter(char c) Determines whether the specified Unicode character is a letter.
bool IsLetterOrDigit(char c) Determines whether the specified Unicode character is a letter or a decimal digit.
bool IsLower(char c) Determines whether the specified Unicode character is a lowercase letter.
bool IsNumber(char c) Determines whether the specified Unicode character is a number.
bool IsPunctuation(char c) Determines whether the specified Unicode character is punctuation.
bool IsSeparator(char c) Determines whether the specified Unicode character is a separator character.
bool IsSymbol(char c) Determines whether the specified Unicode character is a symbol.
bool IsUpper(char c) Determines whether the specified Unicode character is an uppercase letter.
char ToLower(char c) Converts the specified Unicode character to its lowercase equivalent.
char ToUpper(char c) Converts the specified Unicode character to its uppercase equivalent.
string ToString(char c) Converts the specified Char object to its equivalent string representation.

Instance Methods

Signature Description
int CompareTo(object value) Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
int CompareTo(char other) Compares the current instance with another Char object and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
bool Equals(object obj) Returns a value that indicates whether this instance is equal to a specified object.
bool Equals(char obj) Returns a value that indicates whether this instance is equal to a specified Char object.
TypeCode GetTypeCode() Returns the TypeCode for value type Char.
int GetHashCode() Returns the hash code for this instance.
string ToString() Converts the value of this instance to its equivalent string representation.

Operators

Signature Description
bool ==(char c1, char c2) Compares two Char values for equality.
bool !=(char c1, char c2) Compares two Char values for inequality.
bool <(char c1, char c2) Compares two Char values to determine if the first is less than the second.
bool >(char c1, char c2) Compares two Char values to determine if the first is greater than the second.
bool <=(char c1, char c2) Compares two Char values to determine if the first is less than or equal to the second.
bool >=(char c1, char c2) Compares two Char values to determine if the first is greater than or equal to the second.

Requirements

Namespace: System

Assembly: System.Runtime.dll

.NET Standard

Supported in: 2.0

.NET Core

Supported in: 2.0, 2.1, 2.2, 3.0, 3.1

.NET Framework

Supported in: 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8