Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Characters

In the .NET Framework, characters are always represented in 16-bit Unicode code values, easing the development of global applications. A character is represented with an instance of the System.Char structure (a value type). The System.Char type is pretty simple. It offers two public read-only con- stant fields: MinValue, defined as '\0', and MaxValue, defined as '\uffff'.

Given an instance of a Char, you can call the static GetUnicodeCategory method, which returns a value of the System.Globalization.UnicodeCategory enumerated type. This value indicates whether the character is a control character, a currency symbol, a lowercase letter, an uppercase letter, a punctuation character, a math symbol, or another character (as defined by the Unicode standard).


To ease developing, the Char type also offers several static methods, such as IsDigit, IsLetter, IsWhiteSpace, IsUpper, IsLower, IsPunctuation, IsLetterOrDigit, IsControl, IsNumber, IsSeparator, IsSurrogate, IsLowSurrogate, IsHighSurrogate, and IsSymbol. Most of these methods call GetUnicodeCategory internally and simply return true or false accordingly. Note that all of these methods take either a single character for a parameter or a String and the index of a character within the String as parameters.

In addition, you can convert a single character to its lowercase or uppercase equivalent in a culture-agnostic way by calling the static ToLowerInvariant or ToUpperInvariant method. Alternatively, the ToLower and ToUpper methods convert the character by using the culture in- formation associated with the calling thread (which the methods obtain internally by querying the static CurrentCulture property of the System.Globalization.CultureInfo class). You can also specify a particular culture by passing an instance of the CultureInfo class to these methods. ToLower and ToUpper require culture information because letter casing is a culture-dependent op- eration. For example, Turkish considers the uppercase of U+0069 (LATIN LOWERCASE LETTER I) to be U+0130 (LATIN UPPERCASE LETTER I WITH DOT ABOVE), whereas other cultures consider the result to be U+0049 (LATIN CAPITAL LETTER I).

Besides these static methods, the Char type also offers a few instance methods of its own. The Equals method returns true if two Char instances represent the same 16-bit Unicode code point. The CompareTo methods (defined by the IComparable/IComparable<Char> interfaces) return a comparison of two Char instances; this comparison is not culture-sensitive. The ConvertFromUtf32 method produces a string consisting of one or two UTF-16 characters from a single UTF-32 character. The ConvertToUtf32 produces a UTF-32 character from a low/high surrogate pair or from a string. The ToString method returns a String consisting of a single character. The opposite of ToString is Parse/TryParse, which takes a single-character String and returns its UTF-16 code point.

The last method, GetNumericValue, returns the numeric equivalent of a character. I demonstrate this method in the following code.



 

using System;

 

public static class Program { public static void Main() {

Double d; // '\u0033' is the "digit 3" d = Char.GetNumericValue('\u0033'); // '3' would work too Console.WriteLine(d.ToString()); // Displays "3"

 

// '\u00bc' is the "vulgar fraction one quarter ('¼')" d = Char.GetNumericValue('\u00bc'); Console.WriteLine(d.ToString()); // Displays "0.25"

 

// 'A' is the "Latin capital letter A" d = Char.GetNumericValue('A');

Console.WriteLine(d.ToString()); // Displays "­1"

}

}


Finally, three techniques allow you to convert between various numeric types to Char instances and vice versa. The techniques are listed here in order of preference:

CastingThe easiest way to convert a Char to a numeric value such as an Int32 is simply by casting. Of the three techniques, this is the most efficient because the compiler emits Interme- diate Language (IL) instructions to perform the conversion, and no methods have to be called. In addition, some languages (such as C#) allow you to indicate whether the conversion should be performed using checked or unchecked code (discussed in Chapter 5, “Primitive, Reference, and Value Types”).

Use the Convert typeThe System.Convert type offers several static methods that are capable of converting a Char to a numeric type and vice versa. All of these methods perform the conversion as a checked operation, causing an OverflowException to be thrown if the conversion results in the loss of data.

Use the IConvertible interfaceThe Char type and all of the numeric types in the .NET Framework Class Library (FCL) implement the IConvertible interface. This interface defines methods such as ToUInt16 and ToChar. This technique is the least efficient of the three because calling an interface method on a value type requires that the instance be boxed— Char and all of the numeric types are value types. The methods of IConvertible throw a System.InvalidCastException if the type can’t be converted (such as converting a Char to a Boolean) or if the conversion results in a loss of data. Note that many types (including the FCL’s Char and numeric types) implement IConvertible’s methods as explicit interface member implementations (described in Chapter 13, “Interfaces”). This means that you must explicitly cast the instance to an IConvertible before you can call any of the interface’s methods. All of the methods of IConvertible except GetTypeCode accept a reference to an object that implements the IFormatProvider interface. This parameter is useful if for some reason the conversion needs to take culture information into account. For most conversions, you can pass null for this parameter because it would be ignored anyway.

 

The following code demonstrates how to use these three techniques.

 

using System;

 

public static class Program { public static void Main() {

Char c; Int32 n;

 

// Convert number <­> character using C# casting c = (Char) 65;

Console.WriteLine(c); // Displays "A"

 

n = (Int32) c;

Console.WriteLine(n); // Displays "65"

 

c = unchecked((Char) (65536 + 65)); Console.WriteLine(c); // Displays "A"


// Convert number <­> character using Convert c = Convert.ToChar(65);

Console.WriteLine(c); // Displays "A"

 

n = Convert.ToInt32(c);

Console.WriteLine(n); // Displays "65"

 

// This demonstrates Convert's range checking try {

c = Convert.ToChar(70000); // Too big for 16 bits Console.WriteLine(c); // Doesn't execute

}

catch (OverflowException) {

Console.WriteLine("Can't convert 70000 to a Char.");

}

 

// Convert number <­> character using IConvertible c = ((IConvertible) 65).ToChar(null);

Console.WriteLine(c); // Displays "A"

 

n = ((IConvertible) c).ToInt32(null); Console.WriteLine(n); // Displays "65"

}

}

 


Date: 2016-03-03; view: 770


<== previous page | next page ==>
Nbsp;   Design: Base Class or Interface? | Nbsp;   The System.String Type
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.012 sec.)