C# - Char Struct
In C#, the Char struct is used to represent a single Unicode character as a UTF-16 code unit, defined under the System namespace. A Char in C# is a 16-bit value, and it can represent characters in the Basic Multilingual Plane (BMP). Characters beyond 0xFFFF are represented by surrogate pairs (two Char values).
- Char Struct Range: The Char range in C# is from 0x0000 to 0xFFFF. For characters outside this range, UTF-16 encoding uses a surrogate pair of Char values.
- Unicode: Represent a Unicode character in the .NET Framework using a unique 21-bit code point as defined by the Unicode Standard.
- Methods: The Char structure provides methods to convert, compare, and check the Unicode category of a Char object.
Fields
- MaxValue: It is a constant field that represents the largest possible value of a Char.
- MinValue: It is a constant field that represents the smallest possible value of a Char.
Example:
using System;
public class Geeks
{
public static void Main()
{
// Storing first character of string
char c = "Hello"[0];
// Stroring a character in char struct
char c2 = 'h';
Console.WriteLine(c+ " "+c2);
}
}
Output
H h
Methods
Methods | Description |
---|---|
Compares this char with another, returning a negative value if less, zero if equal, and positive if greater. | |
Converts the specified Unicode code point into a UTF-16 encoded string. | |
ConvertToUtf32() | Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point. |
Returns a value that indicates whether this instance is equal to a specified object or Char value. | |
Returns the hash code for this instance. | |
Converts a specified numeric Unicode character to a double-precision floating-point number. | |
Returns the TypeCode for value type Char. | |
Categorizes a Unicode character into a group identified by one of the UnicodeCategory values. | |
Indicates whether a specified Unicode character is categorized as a control character. | |
Indicates whether a Unicode character is categorized as a decimal digit. | |
Indicates whether the specified Char object is a high surrogate. | |
Indicates whether a Unicode character is categorized as a Unicode letter. | |
Indicates whether a Unicode character is categorized as a letter or a decimal digit. | |
Indicates whether a Unicode character is categorized as a lowercase letter. | |
Indicates whether the specified Char object is a low surrogate. | |
Indicates whether a Unicode character is categorized as a number. | |
Indicates whether a Unicode character is categorized as a punctuation mark. | |
Indicates whether a Unicode character is categorized as a separator character. | |
Indicates whether a character has a surrogate code unit. | |
Indicates whether two specified Char objects form a surrogate pair. | |
Indicates whether a Unicode character is categorized as a symbol character. | |
Indicates whether a Unicode character is categorized as an uppercase letter. | |
Indicates whether a Unicode character is categorized as white space. | |
Converts the value of the specified string to its equivalent Unicode character. | |
ToLower() | Converts the value of a Unicode character to its lowercase equivalent. |
Converts the value of a Unicode character to lowercase equivalent using the casing rules of the invariant culture. | |
Converts the value of this instance to its equivalent string representation. | |
ToUpper() | Converts the value of a Unicode character to its uppercase equivalent. |
Converts the value of a Unicode character to its uppercase equivalent using the casing rules of the invariant culture. | |
TryParse(String, Char) | Converts the value of the specified string to its equivalent Unicode character. A return code indicates whether the conversion succeeded or failed. |
Example 1: Using Char.CompareTo()
// C# program to demonstrate the
// Char.CompareTo(Char) Method
using System;
class Geeks
{
public static void Main()
{
char c1 = 'G';
char c2 = 'f';
char c3 = 'M';
// using Char.CompareTo(Char) Method
// returns 0 as this instance has
// same position in the sort as in c1
Console.WriteLine('G'.CompareTo(c1));
// returns -31 as this instance
// precedes c2
Console.WriteLine('G'.CompareTo(c2));
// returns -6 as this instance follows
// c3
Console.WriteLine('G'.CompareTo(c3));
}
}
Output
0 -31 -6
Example 2: Using Char.IsWhiteSpace()
// C# program to illustrate the
// Char.IsWhiteSpace(Char) Method
using System;
class Geeks
{
static public void Main()
{
// Declaration of data type
bool output;
// checking if space
// is a whitespace
char c1 = ' ';
output = Char.IsWhiteSpace(c1);
Console.WriteLine(output);
// checking if carriage return
// is a whitespace
char c2 = '\n';
output = Char.IsWhiteSpace(c2);
Console.WriteLine(output);
// checking if hyphen
// is a whitespace
char c3 = '-';
output = Char.IsWhiteSpace(c3);
Console.WriteLine(output);
}
}
Output
True True False