The .NET Structures Corresponding to the Built-in Value Types


The structures that correspond to C#’s built-in value types were introduced in Chapter 14 when they were used to convert strings holding human-readable numeric values into their equivalent binary values. Here these structures are examined in detail.

The .NET structure names and their C# keyword equivalents are shown in the following table:

.NET Structure Name

C# Name

System.Boolean

bool

System.Char

char

System.Decimal

decimal

System.Double

double

System.Single

float

System.Int16

short

System.Int32

int

System.Int64

long

System.UInt16

ushort

System.UInt32

uint

System.UInt64

ulong

System.Byte

byte

System.Sbyte

sbyte

By using the members defined by these structures, you can perform operations relating to the value types. The following sections examine each of these structures.

Note 

Some methods defined by the structures that correspond to the built-in value types take a parameter of type IFormatProvider or NumberStyles. IFormatProvider is briefly described later in this chapter. NumberStyles is an enumeration found in the System.Globalization namespace. The topic of formatting is discussed in Chapter 21.

The Integer Structures

The integer structures are

Byte

SByte

Int16

UInt16

Int32

UInt32

Int64

UInt64

Each of these structures contains the same methods. They are shown in Table 20-2. The only difference from structure to structure is the type of data acted upon. For example, in each structure, Parse( ) returns a value of the type represented by the structure. In the case of Int32, Parse( ) returns an int value. For UInt16, Parse( ) returns a ushort value. (For an example that demonstrates Parse( ), see Chapter 14.)

Table 20-2: Methods Supported by the Integer Structures

Method

Meaning

public int CompareTo(object v)

Compares the numerical value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.

public int CompareTo(type v)

Compares the numerical value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. In this version of CompareTo( ), type explicitly specifies the data type, such as in int CompareTo(int v). (Added by C# 2.0.)

public override bool Equals(object v)

Returns true if the value of the invoking object equals the value of v.

public bool Equals(type v)

Returns true if the value of the invoking object equals the value of v. In this version of Equals( ), type explicitly specifies the data type, such as in System.Int32.Equals(int v). (Added by C# 2.0.)

public override int GetHashCode( )

Returns the hash code for the invoking object.

public TypeCode GetTypeCode( )

Returns the TypeCode enumeration value for the equivalent value type. For example, for Int32, the type code is TypeCode.Int32.

public static retType Parse(string str)

Returns the binary equivalent of the numeric string in str. If the string does not represent a numeric value as defined by the structure type, an exception is thrown. retType is a placeholder for the actual type of data returned based upon which numeric structure is used. For example, for Int32, retType will be int.

public static retType Parse(string str, IFormatProvider fmtpvdr)

Returns the binary equivalent of the numeric string in str using the culture-specific information provided by fmtpvdr. If the string does not represent a numeric value as defined by the structure type, an exception is thrown. retType is a placeholder for the actual type of data returned based upon which numeric structure is used. For example, for Int32, retType will be int.

public static retType Parse(string str, NumberStyles styles)

Returns the binary equivalent of the numeric string in str using the style information provided by styles. If the string does not represent a numeric value as defined by the structure type, an exception is thrown. retType is a placeholder for the actual type of data returned based upon which numeric structure is used. For example, for Int32, retType will be int.

public static retType Parse(string str, NumberStyles styles, IFormatProvider fmtpvdr)

Returns the binary equivalent of the numeric string in str using the style information provided by styles and the culture-specific format information provided by fmtpvdr. If the string does not represent a numeric value as defined by structure type, an exception is thrown. retType is a placeholder for the actual type of data returned based upon which numeric structure is used. For example, for Int32, retType will be int.

public override string ToString( )

Returns the string representation of the value of the invoking object.

public string ToString(string format)

Returns the string representation of the value of the invoking object as specified by the format string passed in format.

public string ToString(IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr.

public string ToString(string format, IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr and the format specified by format.

public static bool TryParse(string str, out type val)

Attempts to convert the numeric string in str into a binary value. If successful, the value is stored in val and true is returned. If no conversion takes place, false is returned. This differs from Parse( ), which throws an exception on failure. In TryParse( ), type explicitly specifies the data type, such as int. (Added by C# 2.0.)

public static bool TryParse(string str, NumberStyles styles, IFormatProvider fmtpvdr, out type val)

Attempts to convert the numeric string in str into a binary value using the style information provided by styles and the culture-specific format information provided by fmtpvdr. If successful, the value is stored in val and true is returned. If no conversion takes place, false is returned. This differs from Parse( ), which throws an exception on failure. In TryParse( ), type explicitly specifies the data type, such as int. (Added by C# 2.0.)

In addition to the methods shown in Table 20-2, the integer structures also define the following const fields:

 MaxValue MinValue

For each structure, these fields contain the largest and smallest value that that type of integer can hold.

All of the integer structures implement the following interfaces: IComparable, IComparable<T>, IConvertible, IFormattable, and IEquatable<T>. The generic interfaces are implemented specifically for each integer structure. For example, Int32 implements IComparable<int>.

The Floating-Point Structures

There are two floating-point structures: Double and Single. Single represents float. Its methods are shown in Table 20-3, and its fields are shown in Table 20-4. Double represents double. Its methods are shown in Table 20-5, and its fields are shown in Table 20-6. As is the case with the integer structures, you can specify culture-specific information and format information in a call to Parse( ), TryParse( ), or ToString( ).

Table 20-3: The Methods Supported by Single

Method

Meaning

public int CompareTo(object v)

Compares the numerical value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.

public int CompareTo(float v)

Compares the numerical value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. (Added by C# 2.0.)

public override bool Equals(object v)

Returns true if the value of the invoking object equals the value of v.

public bool Equals(float v)

Returns true if the value of the invoking object equals the value of v. (Added by C# 2.0.)

public override int GetHashCode( )

Returns the hash code for the invoking object.

public TypeCode GetTypeCode( )

Returns the TypeCode enumeration value for Single, which is TypeCode.Single.

public static bool IsInfinity(float v)

Returns true if v represents infinity (either positive or negative). Otherwise, returns false.

public static bool IsNaN(float v)

Returns true if v is not a number. Otherwise, returns false.

public static bool IsPositiveInfinity(float v)

Returns true if v represents positive infinity. Otherwise, returns false.

public static bool IsNegativeInfinity(float v)

Returns true if v represents negative infinity. Otherwise, returns false.

public static float Parse(string str)

Returns the binary equivalent of the numeric string in str. If the string does not represent a float value, an exception is thrown.

public static float Parse(string str, IFormatProvider fmtpvdr)

Returns the binary equivalent of the numeric string in str using the culture-specific information provided by fmtpvdr. If the string does not represent a float value, an exception is thrown.

public static float Parse(string str, NumberStyles styles)

Returns the binary equivalent of the numeric string in str using the style information provided by styles. If the string does not represent a float value, an exception is thrown.

public static float Parse(string str, NumberStyles styles, IFormatProvider fmtpvdr)

Returns the binary equivalent of the numeric string in str using the style information provided by styles and the culture-specific format information provided by fmtpvdr. If the string does not represent a float value, an exception is thrown.

public override string ToString( )

Returns the string representation of the value of the invoking object.

public string ToString(string format)

Returns the string representation of the value of the invoking object as specified by the format string passed in format.

public string ToString(IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr.

public string ToString(string format, IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr and the format specified by format.

public static bool TryParse(string str, out float val)

Attempts to convert the numeric string in str into a float value. If successful, the value is stored in val and true is returned. If no conversion takes place, false is returned. This differs from Parse( ), which throws an exception on failure. (Added by C# 2.0.)

public static bool TryParse(string str, NumberStyles styles, IFormatProvider fmtpvdr, out float val)

Attempts to convert the numeric string in str into a float value using the style information provided by styles and the culture-specific format information provided by fmtpvdr. If successful, the value is stored in val and true is returned. If no conversion takes place, false is returned. This differs from Parse( ), which throws an exception on failure. (Added by C# 2.0.)

Table 20-4: The Fields Supported by Single

Field

Meaning

public const float Epsilon

The smallest non-zero positive value.

public const float MaxValue

The largest value that a float can hold.

public const float MinValue

The smallest value that a float can hold.

public const float NaN

A value that is not a number.

public const float NegativeInfinity

A value representing negative infinity.

public const float PositiveInfinity

A value representing positive infinity.

Table 20-5: The Methods Supported by Double

Method

Meaning

public int CompareTo(object v)

Compares the numerical value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.

public int CompareTo(double v)

Compares the numerical value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. (Added by C# 2.0.)

public override bool Equals(object v)

Returns true if the value of the invoking object equals the value of v.

public bool Equals(double v)

Returns true if the value of the invoking object equals the value of v. (Added by C# 2.0.)

public override int GetHashCode( )

Returns the hash code for the invoking object.

public TypeCode GetTypeCode( )

Returns the TypeCode enumeration value for Double, which is TypeCode.Double.

public static bool IsInfinity(double v)

Returns true if v represents infinity (either positive or negative). Otherwise, returns false.

public static bool IsNaN(double v)

Returns true if v is not a number. Otherwise, returns false.

public static bool IsPositiveInfinity(double v)

Returns true if v represents positive infinity. Otherwise, returns false.

public static bool IsNegativeInfinity(double v)

Returns true if v represents negative infinity. Otherwise, returns false.

public static double Parse(string str)

Returns the binary equivalent of the numeric string in str. If the string does not represent a double value, an exception is thrown.

public static double Parse(string str, IFormatProvider fmtpvdr)

Returns the binary equivalent of the numeric string in str using the culture-specific information provided by fmtpvdr. If the string does not represent a double value, an exception is thrown.

public static double Parse(string str, NumberStyles styles)

Returns the binary equivalent of the numeric string in str using the style information provided by styles. If the string does not represent a double value, an exception is thrown.

public static double Parse(string str, NumberStyles styles, IFormatProvider fmtpvdr)

Returns the binary equivalent of the numeric string in str using the style information provided by styles and the culture-specific format information provided by fmtpvdr. If the string does not represent a double value, an exception is thrown.

public override string ToString( )

Returns the string representation of the value of the invoking object.

public string ToString(string format)

Returns the string representation of the value of the invoking object as specified by the format string passed in format.

public string ToString(IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr.

public string ToString(string format, IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr and the format specified by format.

public static bool TryParse(string str, out double val)

Attempts to convert the numeric string in str into a double value. If successful, the value is stored in val and true is returned. If no conversion takes place, false is returned. This differs from Parse( ), which throws an exception on failure. (Added by C# 2.0.)

public static bool TryParse(string str, NumberStyles styles, IFormatProvider fmtpvdr, out double val)

Attempts to convert the numeric string in str into a double value using the style information provided by styles and the culture-specific format information provided by fmtpvdr. If successful, the value is stored in val and true is returned. If no conversion takes place, false is returned. This differs from Parse( ), which throws an exception on failure. (Added by C# 2.0.)

Table 20-6: Fields Supported by Double

Field

Meaning

public const double Epsilon

The smallest non-zero positive value.

public const double MaxValue

The largest value that a double can hold.

public const double MinValue

The smallest value that a double can hold.

public const double NaN

A value that is not a number.

public const double NegativeInfinity

A value representing negative infinity.

public const double PositiveInfinity

A value representing positive infinity.

The floating-point structures implement the following interfaces: IComparable, IComparable<T>, IConvertible, IFormattable, and IEquatable<T>. The generic interfaces are implemented specifically for each floating-point structure. For example, Double implements IComparable<double>.

Decimal

The Decimal structure is a bit more complicated than its integer and floating-point relatives. It contains many constructors, fields, methods, and operators that help integrate decimal with the other numeric types supported by C#. For example, several of the methods provide conversions between decimal and the other numeric types.

Decimal offers eight public constructors. The following six are the most commonly used:

 public Decimal(int v) public Decimal(uint v) public Decimal(long v) public Decimal(ulong v) public Decimal(float v) public Decimal(double v)

Each constructs a Decimal from the specified value.

You can also construct a Decimal by specifying its constituent parts using this constructor:

 public Decimal(int low, int middle, int high, bool signFlag, byte scaleFactor)

A decimal value consists of three parts. The first is a 96-bit integer, the second is a sign flag, and the third is a scaling factor. The 96-bit integer is passed in 32-bit chunks through low, middle, and high. The sign is passed through signFlag, which is false for a positive number and true for a negative number. The scaling factor is passed in scaleFactor, which must be a value between 0 and 28. This factor specifies the power of 10 (that is, 10scaleFactor) by which the number is divided, thus yielding its fractional component.

Instead of passing each component separately, you can specify the constituents of a Decimal in an array of integers, using this constructor:

 public Decimal(int[ ] parts)

The first three ints in parts contain the 96-bit integer value. In parts[3], bit 31 contains the sign flag (zero for positive, 1 for negative), and bits 16 through 23 contain the scale factor.

Decimal implements the following interfaces: IComparable, IComparable<decimal>, IConvertible, and IFormattable, and IEquatable<decimal>.

Here is an example that constructs a decimal value by hand:

 // Manually create a decimal number. using System; class CreateDec {   public static void Main() {     decimal d = new decimal(12345, 0, 0, false, 2);     Console.WriteLine(d);   } }

The output is shown here:

 123.45

In this example, the value of the 96-bit integer is 12345. Its sign is positive, and it has two decimal fractions.

The methods defined by Decimal are shown in Table 20-7. The fields defined by Decimal are shown in Table 20-8. Decimal also defines a large number of operators and conversions that allow decimal values to be used in expressions with other numeric types. The rules governing the use of decimal in expressions and assignments are described in Chapter 3.

Table 20-7: The Methods Defined by Decimal

Method

Meaning

public static decimal Add(decimal v1, decimal v2)

Returns v1 + v2.

public static decimal Ceiling(decimal v)

Returns the smallest integer (represented as a decimal value) not less than v. For example, given 1.02, Ceiling( ) returns 2.0. Given 1.02, Ceiling( ) returns 1. (Added by C# 2.0.)

public static int Compare(decimal v1, decimal v2)

Compares the numerical value of v1 with that of v2. Returns zero if the values are equal. Returns a negative value if v1 is less than v2. Returns a positive value if v1 is greater than v2.

public int CompareTo(object v)

Compares the numerical value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.

public int CompareTo(decimal v)

Compares the numerical value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. (Added by C# 2.0.)

public static decimal Divide(decimal v1, decimal v2)

Returns v1 / v2.

public bool Equals(decimal v)

Returns true if the value of the invoking object equals the value of v. (Added by C# 2.0.)

public override bool Equals(object v)

Returns true if the value of the invoking object equals the value of v.

public static bool Equals(decimal v1, decimal v2)

Returns true if v1 equals v2.

public static decimal Floor(decimal v)

Returns the largest integer (represented as a decimal value) not greater than v. For example, given 1.02, Floor( ) returns 1.0. Given 1.02, Floor( ) returns 2.

public static decimal

FromOACurrency(long v)

Converts the OLE Automation value in v into its decimal equivalent and returns the result.

public static int[ ] GetBits(decimal v)

Returns the binary representation of v and returns it in an array of ints. The organization of this array is as described in the text.

public override int GetHashCode( )

Returns the hash code for the invoking object.

public TypeCode GetTypeCode( )

Returns the TypeCode enumeration value for Decimal, which is TypeCode.Decimal.

public static decimal Multiply(decimal v1, decimal v2)

Returns v1 * v2.

public static decimal Negate(decimal v)

Returns v.

public static decimal Parse(string str)

Returns the binary equivalent of the numeric string in str. If the string does not represent a decimal value, an exception is thrown.

public static decimal Parse(string str, IFormatProvider fmtpvdr)

Returns the binary equivalent of the numeric string in str using the culture-specific information provided by fmtpvdr. If the string does not represent a decimal value, an exception is thrown.

public static decimal Parse(string str, NumberStyles styles)

Returns the binary equivalent of the numeric string in str, using the style information provided by styles. If the string does not represent a decimal value, an exception is thrown.

public static decimal Parse(string str, NumberStyles styles, IFormatProvider fmtpvdr)

Returns the binary equivalent of the numeric string in str using the style information provided by styles and the culture-specific format information provided by fmtpvdr. If the string does not represent a decimal value, an exception is thrown.

public static decimal Remainder(decimal v1, decimal v2)

Returns the remainder of the integer division v1 / v2.

public static decimal Round(decimal v)

Returns the value of v rounded to the nearest whole number. (Added by C# 2.0.)

public static decimal Round(decimal v, int decPlaces)

Returns the value of v rounded to the number of decimal places specified by decPlaces, which must be between 0 and 28.

public static decimal Round(decimal v, MidPointRounding how)

Returns the value of v rounded to the nearest whole number using the rounding mode specified by how. The rounding mode applies only to those conditions in which v is at the midpoint between two whole numbers. (Added by C# 2.0.)

public static decimal Round(decimal v, int decPlaces, MidPointRounding how)

Returns the value of v rounded to the number of decimal places specified by decPlaces (which must be between 0 and 28) using the rounding mode specified by how. The rounding mode applies only to those conditions in which v is at the midpoint between two rounded values. (Added by C# 2.0.)

public static decimal Subtract(decimal v1, decimal v2)

Returns v1 v2.

public static byte ToByte(decimal v)

Returns the byte equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a byte.

public static double ToDouble(decimal v)

Returns the double equivalent of v. A loss of precision may occur because double has fewer significant digits than does decimal.

public static short ToInt16(decimal v)

Returns the short equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a short.

public static int ToInt32(decimal v)

Returns the int equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of an int.

public static long ToInt64(decimal v)

Returns the long equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a long.

public static long ToOACurrency(decimal v)

Converts v into the equivalent OLE Automation value and returns the result.

public static sbyte ToSByte(decimal v)

Returns the sbyte equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of an sbyte.

public static float ToSingle(decimal v)

Returns the float equivalent of v. A loss of precision may occur because float has fewer significant digits than does decimal.

public override string ToString( )

Returns the string representation of the value of the invoking object.

public string ToString(string format)

Returns the string representation of the value of the invoking object as specified by the format string passed in format.

public string ToString(IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr.

public string ToString(string format, IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr and the format specified by format.

public static ushort ToUInt16(decimal v)

Returns the ushort equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a ushort.

public static uint ToUInt32(decimal v)

Returns the uint equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a uint.

public static ulong ToUInt64(decimal v)

Returns the ulong equivalent of v. Any fractional component is truncated. An OverflowException occurs if v is not within the range of a ulong.

public static decimal Truncate(decimal v)

Returns the whole-number portion of v. Thus, it truncates any fractional digits.

public static bool TryParse(string str, out decimal val)

Attempts to convert the numeric string in str into a decimal value. If successful, the value is stored in val and true is returned. If no conversion takes place, false is returned. This differs from Parse( ), which throws an exception on failure. (Added by C# 2.0.)

public static bool TryParse(string str, NumberStyles styles, IFormatProvider fmtpvdr, out decimal val)

Attempts to convert the numeric string in str into a decimal value using the style information provided by styles and the culture-specific format information provided by fmtpvdr. If successful, the value is stored in val and true is returned. If no conversion takes place, false is returned. This differs from Parse( ), which throws an exception on failure. (Added by C# 2.0.)

Table 20-8: Fields Supported by Decimal

Field

Meaning

public static readonly decimal MaxValue

The largest value that a decimal can hold.

public static readonly decimal MinusOne

The decimal representation of 1.

public static readonly decimal MinValue

The smallest value that a decimal can hold.

public static readonly decimal One

The decimal representation of 1.

public static readonly decimal Zero

The decimal representation of 0.

Char

The structure corresponding to the char type is Char. It is quite useful because it supplies a large number of methods that allow you to process and categorize characters. For example, you can convert a lowercase character to uppercase by calling ToUpper( ). You can determine if a character is a digit by calling IsDigit( ).

The methods defined by Char are shown in Table 20-9. Notice that several, such as ConvertFromUtf32( ) and ConvertToUtf32( ), give you the ability to work with both UTF-16 and UTF-32 Unicode characters. In the past, all Unicode characters could be represented by 16 bits, which is the size of a char. However, recently the Unicode character set was expanded, and more than 16 bits are required. Each Unicode character is represented by a code point. The way that a code point is encoded depends on the Unicode Transformation Format (UTF) being used. In UTF-16, each code point requires two 16-bit values. In UTF-32, each code point requires one 32-bit value. Because a code point has a value greater than a char can hold, two chars are used to represent it. The first character is called the high surrogate, and the second is called the low surrogate. The new methods in Char provide the necessary conversions between UTF-16 and UTF-32.

Table 20-9: Methods Defined by Char

Method

Meaning

public int CompareTo(char v)

Compares the character in the invoking object with that of v. Returns zero if the characters are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. (Added by C# 2.0.)

public int CompareTo(object v)

Compares the character in the invoking object with that of v. Returns zero if the characters are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.

public static string ConvertFromUtf32(int utf32Ch)

Converts the Unicode UTF-32 code point in utf32Ch into a UTF-16 string and returns the result. (Added by C# 2.0.)

pubic static int ConvertToUtf32(char highSurrogate, char lowSurrogate)

Converts the high and low UTF-16 surrogates specified by highSurrogate and lowSurrogate into a UTF-32 code point. The result is returned. (Added by C# 2.0.)

pubic static int ConvertToUtf32(string str, int idx)

Converts the UTF-16 surrogate pair at str[idx] into its UTF-32 code point. The result is returned. (Added by C# 2.0.)

public bool Equals(char v)

Returns true if the value of the invoking object equals the value of v. (Added by C# 2.0.)

public override bool Equals(object v)

Returns true if the value of the invoking object equals the value of v.

public override int GetHashCode( )

Returns the hash code for the invoking object.

public static double GetNumericValue(char ch)

Returns the numeric value of ch if ch is a digit. Otherwise, returns 1.

public static double GetNumericValue(string str, int idx)

Returns the numeric value of str[idx] if that character is a digit. Otherwise, returns 1.

public TypeCode GetTypeCode( )

Returns the TypeCode enumeration value for Char, which is TypeCode.Char.

public static UnicodeCategory GetUnicodeCategory(char ch)

Returns the UnicodeCategory enumeration value for ch. UnicodeCategory is an enumeration defined by System.Globalization that categorizes Unicode characters.

public static UnicodeCategory GetUnicodeCategory(string str, int idx)

Returns the UnicodeCategory enumeration value for str[idx]. UnicodeCategory is an enumeration defined by System.Globalization that categorizes Unicode characters.

public static bool IsControl(char ch)

Returns true if ch is a control character. Otherwise, returns false.

public static bool IsControl(string str, int idx)

Returns true if str[idx] is a control character. Otherwise, returns false.

public static bool IsDigit(char ch)

Returns true if ch is a digit. Otherwise, returns false.

public static bool IsDigit(string str, int idx)

Returns true if str[idx] is a digit. Otherwise, returns false.

public static bool IsHighSurrogate(char ch)

Returns true if ch is a valid UTF-32 high surrogate. Otherwise, returns false. (Added by C# 2.0.)

public static bool IsHighSurrogate(string str, int idx)

Returns true if str[idx] is a valid UTF-32 high surrogate. Otherwise, returns false. (Added by C# 2.0.)

public static bool IsLetter(char ch)

Returns true if ch is a letter of the alphabet. Otherwise, returns false.

public static bool IsLetter(string str, int idx)

Returns true if str[idx] is a letter of the alphabet. Otherwise, returns false.

public static bool IsLetterOrDigit(char ch)

Returns true if ch is either a letter of the alphabet or a digit. Otherwise, returns false.

public static bool IsLetterOrDigit(string str, int idx)

Returns true if str[idx] is either a letter of the alphabet or a digit. Otherwise, returns false.

public static bool IsLower(char ch)

Returns true if ch is a lowercase letter of the alphabet. Otherwise, returns false.

public static bool IsLower(string str, int idx)

Returns true if str[idx] is a lowercase letter of the alphabet. Otherwise, returns false.

public static bool IsLowSurrogate(char ch)

Returns true if ch is a valid UTF-32 low surrogate. Otherwise, returns false. (Added by C# 2.0.)

public static bool IsLowSurrogate(string str, int idx)

Returns true if str[idx] is a valid UTF-32 low surrogate. Otherwise, returns false. (Added by C# 2.0.)

public static bool IsNumber(char ch)

Returns true if ch is a number. Otherwise, returns false.

public static bool IsNumber(string str, int idx)

Returns true if str[idx] is a number. Otherwise, returns false.

public static bool IsPunctuation(char ch)

Returns true if ch is a punctuation character. Otherwise, returns false.

public static bool IsPunctuation(string str, int idx)

Returns true if str[idx] is a punctuation character. Otherwise, returns false.

public static bool IsSeparator(char ch)

Returns true if ch is a separator character, such as a space. Otherwise, returns false.

public static bool IsSeparator(string str, int idx)

Returns true if str[idx] is a separator character, such as a space. Otherwise, returns false.

public static bool IsSurrogate(char ch)

Returns true if ch is a Unicode surrogate character. Otherwise, returns false.

public static bool IsSurrogate(string str, int idx)

Returns true if str[idx] is a Unicode surrogate character. Otherwise, returns false.

public static bool IsSymbol(char ch)

Returns true if ch is a symbolic character, such as the currency symbol. Otherwise, returns false.

public static bool IsSymbol(string str, int idx)

Returns true if str[idx] is a symbolic character, such as the currency symbol. Otherwise, returns false.

public static bool IsUpper(char ch)

Returns true if ch is an uppercase letter. Otherwise, returns false.

public static bool IsUpper(string str, int idx)

Returns true if str[idx] is an uppercase letter. Otherwise, returns false.

public static bool IsWhiteSpace(char ch)

Returns true if ch is a whitespace character, such as a space or tab. Otherwise, returns false.

public static bool IsWhiteSpace(string str, int idx)

Returns true if str[idx] is a whitespace character, such as a space or tab. Otherwise, returns false.

public static char Parse(string str)

Returns the char equivalent of the character in str. If str contains more than one character, a FormatException is thrown.

public static char ToLower(char ch)

Returns the lowercase equivalent of ch if ch is an uppercase letter. Otherwise, ch is returned unchanged.

public static char ToLower(char ch, CultureInfo c)

Returns the lowercase equivalent of ch if ch is an uppercase letter. Otherwise, ch is returned unchanged. The conversion is handled in accordance with the specified cultural information. CultureInfo is a class defined in System.Globalization.

public static char ToLowerInvariant(char ch)

Returns the lowercase version of ch independently of the cultural settings. (Added by C# 2.0.)

public override string ToString( )

Returns the string representation of the value of the invoking Char.

public static string ToString(char ch)

Returns the string representation of ch.

public string ToString(IFormatProvider fmtpvdr)

Returns the string representation of the invoking Char using the specified culture information.

public static char ToUpper(char ch)

Returns the uppercase equivalent of ch if ch is a lowercase letter. Otherwise, ch is returned unchanged.

public static char ToUpper(char ch, CultureInfo c)

Returns the uppercase equivalent of ch if ch is a lowercase letter. Otherwise, ch is returned unchanged. The conversion is handled in accordance with the specified cultural information. CultureInfo is a class defined in System.Globalization.

public static char ToUpperInvariant(char ch)

Returns the uppercase version of ch independently of the cultural settings. (Added by C# 2.0.)

public static bool TryParse(string str, out char ch)

Attempts to convert the character in str into its char equivalent. If successful, the value is stored in ch and true is returned. If str contains more than one character, false is returned. This differs from Parse( ), which throws an exception on failure. (Added by C# 2.0.)

Char defines the following fields:

 public const char MaxValue public const char MinValue

These represent the largest and smallest values that a char variable can hold.

Char implements the following interfaces: IComparable, IComparable<char>, IConvertible, and IEquatable<char>.

Here is a program that demonstrates several of the methods defined by Char:

 // Demonstrate several Char methods. using System; class CharDemo {   public static void Main() {     string str = "This is a test. $23";     int i;     for(i=0; i < str.Length; i++) {       Console.Write(str[i] + " is");       if(Char.IsDigit(str[i]))         Console.Write(" digit");       if(Char.IsLetter(str[i]))         Console.Write(" letter");       if(Char.IsLower(str[i]))         Console.Write(" lowercase");       if(Char.IsUpper(str[i]))         Console.Write(" uppercase");       if(Char.IsSymbol(str[i]))         Console.Write(" symbol");       if(Char.IsSeparator(str[i]))         Console.Write(" separator");       if(Char.IsWhiteSpace(str[i]))         Console.Write(" whitespace");       if(Char.IsPunctuation(str[i]))         Console.Write(" punctuation");       Console.WriteLine();     }     Console.WriteLine("Original: " + str);     // Convert to uppercase.     string newstr = "";     for(i=0; i < str.Length; i++)       newstr += Char.ToUpper(str[i]);     Console.WriteLine("Uppercased: " + newstr);   } }

The output is shown here:

 T is letter uppercase h is letter lowercase i is letter lowercase s is letter lowercase   is separator whitespace i is letter lowercase s is letter lowercase   is separator whitespace a is letter lowercase   is separator whitespace t is letter lowercase e is letter lowercase s is letter lowercase t is letter lowercase . is punctuation   is separator whitespace $ is symbol 2 is digit 3 is digit Original: This is a test. $23 Uppercased: THIS IS A TEST. $23

The Boolean Structure

The Boolean structure supports the bool data type. The methods defined by Boolean are shown in Table 20-10. It defines these fields:

 public static readonly string FalseString public static readonly string TrueString
Table 20-10: Methods Defined by Boolean

Method

Meaning

public int CompareTo(bool v)

Compares the value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object is false and v is true. Returns a positive value if the invoking object is true and v is false. (Added by C# 2.0.)

public int CompareTo(object v)

Compares the value of the invoking object with that of v. Returns zero if the values are equal. Returns a negative value if the invoking object is false and v is true. Returns a positive value if the invoking object is true and v is false.

public bool Equals(bool v)

Returns true if the value of the invoking object equals the value of v. (Added by C# 2.0.)

public override bool Equals(object v)

Returns true if the value of the invoking object equals the value of v.

public override int GetHashCode( )

Returns the hash code for the invoking object.

public TypeCode GetTypeCode( )

Returns the TypeCode enumeration value for Boolean, which is TypeCode.Boolean.

public static bool Parse(string str)

Returns the bool equivalent of the string in str. If the string is neither Boolean.TrueString nor Boolean.FalseString, a FormatException is thrown. However, case differences are ignored.

public override string ToString( )

Returns the string representation of the value of the invoking object.

public string ToString(IFormatProvider fmtpvdr)

Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr.

public static bool TryParse(string str, out bool b)

Attempts to convert the character in str into its bool equivalent. If successful, the value is stored in b and true is returned. If the string is neither Boolean.TrueString nor Boolean.FalseString, false is returned. (Case differences are ignored.) This differs from Parse( ), which throws an exception on failure. (Added by C# 2.0.)

These contain the human-readable forms of true and false. For example, if you output FalseString using a call to WriteLine( ), the string “False” is displayed.

Boolean implements the following interfaces: IComparable, IComparable<bool>, IConvertible, and IEquatable<bool>.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net