C# | Char Struct

In C#, Char Struct is used to represent a character as UTF-16 code unit. This structure is defined under System namespace. Basically, this is used to represent a Unicode character in .NET Framework. A unique 21-bit scalar number which is termed as code point is used by the Unicode Standard to identifies each Unicode character. It also defines the UTF-16 encoding form which specified how a code point can be encoded into a sequence of one or more 16-bit values. Each 16-bit value ranges from hexadecimal 0x0000 through 0xFFFF and is stored in a Char structure. The value of a Char object is its 16-bit numeric (ordinal) value.
The Char structure provides different methods which are used to convert the value of the current Char object to an object of another type, comparing Char objects, and to check the Unicode category of a Char object etc.

Fields:

  • MaxValue: It is a constant field which represents the largest possible value of a Char.
  • MinValue: It is a constant field which represents the smallest possible value of a Char.

Methods

Method Description
CompareTo() Compares this instance to a specified object or value type, and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or value type.
ConvertFromUtf32(Int32) 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.
Equals() Returns a value that indicates whether this instance is equal to a specified object or Char value.
GetHashCode() Returns the hash code for this instance.
GetNumericValue() Converts a specified numeric Unicode character to a double-precision floating-point number.
GetTypeCode() Returns the TypeCode for value type Char.
GetUnicodeCategory() Categorizes a Unicode character into a group identified by one of the UnicodeCategory values.
IsControl() Indicates whether a specified Unicode character is categorized as a control character.
IsDigit() Indicates whether a Unicode character is categorized as a decimal digit.
IsHighSurrogate() Indicates whether the specified Char object is a high surrogate.
IsLetter() Indicates whether a Unicode character is categorized as a Unicode letter.
IsLetterOrDigit() Indicates whether a Unicode character is categorized as a letter or a decimal digit.
IsLower() Indicates whether a Unicode character is categorized as a lowercase letter.
IsLowSurrogate() Indicates whether the specified Char object is a low surrogate.
IsNumber() Indicates whether a Unicode character is categorized as a number.
IsPunctuation() Indicates whether a Unicode character is categorized as a punctuation mark.
IsSeparator() Indicates whether a Unicode character is categorized as a separator character.
IsSurrogate() Indicates whether a character has a surrogate code unit.
IsSurrogatePair() Indicates whether two specified Char objects form a surrogate pair.
IsSymbol() Indicates whether a Unicode character is categorized as a symbol character.
IsUpper() Indicates whether a Unicode character is categorized as an uppercase letter.
IsWhiteSpace() Indicates whether a Unicode character is categorized as white space.
Parse(String) Converts the value of the specified string to its equivalent Unicode character.
ToLower() Converts the value of a Unicode character to its lowercase equivalent.
ToLowerInvariant(Char) Converts the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture.
ToString() Converts the value of this instance to its equivalent string representation.
ToUpper() Converts the value of a Unicode character to its uppercase equivalent.
ToUpperInvariant(Char) 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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Char.CompareTo(Char) Method
using System;
  
class GFG {
  
    // Main Method
    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));
  
        // using Char.CompareTo(Char) Method
        // returns -31 as this instance
        // precedes c2
        Console.WriteLine('G'.CompareTo(c2));
  
        // using Char.CompareTo(Char) Method
        // returns -6 as this instance follows
        // c3
        Console.WriteLine('G'.CompareTo(c3));
    }
}

chevron_right


Output:

0
-31
-6

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Char.IsWhiteSpace(Char) Method
using System;
  
class GFG {
  
    // Main Method
    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);
    }
}

chevron_right


Output:

True
True
False

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.