More Fundamental Types


The fundamental types discussed so far are numeric types. C# includes some additional types as well: bool, char, and string.

Boolean Type (bool)

Another C# primitive is a Boolean or conditional type, bool, which represents true or false in conditional statements and expressions. Allowable values are the keywords TRue and false. The BCL name for bool is System.Boolean. The literal values for a Boolean use the keywords true and false. For example, in order to compare two strings in a case-insensitive manner, you call the string.Compare() method and pass a bool literal of true (see Listing 2.8).

Listing 2.8. A Case-Insensitive Comparison of Two Strings

string option; ... int comparison = string.Compare(option, "/Help", true);

In this case, you make a case-insensitive comparison of the contents of the variable option with the literal text /Help and assign the result to comparison.

Although theoretically a single bit could hold the value of a Boolean, the size of bool is a byte.

Character Type (char)

A char type represents 16-bit characters whose set of possible values corresponds to the Unicode character set. Technically, a char is the same size as a 16-bit unsigned integer (ushort) with values between 0 and 65,535. However, char is a unique type in C# and code should treat it as such.

The BCL name for char is System.Char.

Beginner Topic: The Unicode Standard

Unicode is an international standard for representing characters found in the majority of human languages. It provides computer systems with functionality for building localized applications, applications that display the appropriate language and culture characteristics for different cultures.


Advanced Topic: 16 Bits Is Too Small for All Unicode Characters

Unfortunately, not all Unicode characters are available within a 16-bit char. When Unicode was first started, its designers believed that 16 bits would be enough, but as more languages were supported, it was realized that this assumption was incorrect. The cumbersome result is that some Unicode characters are composed of surrogate char pairs totaling 32 bits.


To enter a literal character type, place the character within single quotes, as in 'A'. Allowable characters comprise the full range of keyboard characters, including letters, numbers, and special symbols.

Some characters cannot be placed directly into the source code and instead require special handling. These characters are prefixed with a backslash (\) followed by a special character code. In combination, the backslash and special character code are an escape sequence. For example, '\n' represents a newline, and '\t' represents a tab. Since a backslash indicates the beginning of an escape sequence, it can no longer identify a simple backslash; instead, you need to use '\\' to represent a single backslash character.

Listing 2.9 writes out one single quote because the character represented by \' corresponds to a single quote.

Listing 2.9. Displaying a Single Quote Using an Escape Sequence

class SingleQuote {  static void Main()  {      System.Console.WriteLine('\'');  } }

In addition to showing the escape sequence, Table 2.4 includes the Unicode representation of characters.

Table 2.4. Escape Characters

Escape Sequence

Character Name

Unicode Encoding

\'

Single quote

0x0027

\"

Double quote

0x0022

\\

Backslash

0x005C

\0

Null

0x0000

\a

Alert (system beep)

0x0007

\b

Backspace

0x0008

\f

Form feed

0x000C

\n

Line feed (sometimes referred to as a newline)

0x000A

\r

Carriage return

0x000D

\t

Horizontal tab

0x0009

\v

Vertical tab

0x000B

\uxxxx

Unicode character in hex

\u0029

\x[n][ n][ n]n

Unicode character in hex (first three placeholders are optional); variable length version of \uxxxx

\x3A

\Uxxxxxxxx

Unicode escape sequence for creating surrogate pairs


You can represent any character using Unicode encoding. To do so, prefix the Unicode value with \u. You represent Unicode characters in hexadecimal notation. The letter A, for example, is the hexadecimal value 0x41; Listing 2.10 uses Unicode characters to display a smiley face (:)), and Output 2.8 shows the results.

Listing 2.10. Using Unicode Encoding to Display a Smiley Face

System.Console.Write('\u003A'); System.Console.WriteLine('\u0029');

Output 2.8.

:)

Strings

The fundamental string type in C# is the data type string, whose BCL name is System.String. The string includes some special characteristics that may be unexpected to developers familiar with other programming languages. The characteristics include a string verbatim prefix character, @, and the fact that a string is immutable.

Literals

You can enter a literal string into code by placing the text in double quotes ("), as you saw in the HelloWorld program. Strings are composed of characters, and because of this, escape sequences can be embedded within a string.

In Listing 2.11, for example, two lines of text are displayed. However, instead of using System.Console.WriteLine(), the code listing shows System.Console.Write() with the newline character, \n. Output 2.9 shows the results.

Listing 2.11. Using the \n Character to Insert a Newline

class DuelOfWits {   static void Main()   {     System.Console.Write(         "\"Truly, you have a dizzying intellect.\"");     System.Console.Write("\n\"Wait 'til I get going!\"\n");   } }

Output 2.9.

"Truly, you have a dizzying intellect." "Wait 'til I get going!"

The escape sequence for double quotes differentiates the printed double quotes from the double quotes that define the beginning and end of the string.

In C#, you can use the @ symbol in front of a string to signify that a backslash should not be interpreted as the beginning of an escape sequence. The resulting verbatim string literal does not reinterpret just the backslash character. Whitespace is also taken verbatim when using the @ string syntax. The triangle in Listing 2.12, for example, appears in the console exactly as typed, including the backslashes, newlines, and indentation. Output 2.10 shows the results.

Listing 2.12. Displaying a Triangle Using a Verbatim String Literal

class Triangle {   static void Main()   {      System.Console.Write( @ "begin             /\            /  \           /    \          /      \         /________\ end");   } }

Output 2.10.

begin          /\         /  \        /    \       /      \      /________\ end

Without the @ character, this code would not even compile. In fact, even if you changed the shape to a square, eliminating the backslashes, the code would still not compile because a newline cannot be placed directly within a string that is not prefaced with the @ symbol.

The only escape sequence the verbatim string does support is "", which signifies double quotes and does not terminate the string.

Language Contrast: C++String Concatenation at Compile Time

Unlike C++, C# does not automatically concatenate literal strings. You cannot, for example, specify a string literal as follows:

"Major Strasser has been shot. " "Round up the usual suspects."


Rather, concatenation requires the use of the addition operator. (If the compiler can calculate the result at compile time, the resulting CIL code will be a single string.)


If the same literal string appears within an assembly multiple times, the compiler will define the string only once within the assembly and all variables will point to the single string literal. That way, if the same string literal containing thousands of characters was placed multiple times into the code, the resulting assembly would reflect the size of only one of them.

String Methods

The string type, like the System.Console type, includes several methods. There are methods, for example, for formatting, concatenating, and comparing strings.

The Format() method in Table 2.5 behaves exactly like the Console.Write() and Console.WriteLine() methods, except that instead of displaying the result in the console window, string.Format() returns the result.

All of these methods are static. This means that, to call the method, it is necessary to prefix the method name with the type that contains the method. Some of the methods in the string class, however, are instance methods. Instead of prefixing the method with the type, instance methods use the variable name (or some other reference to an instance). Table 2.6 shows a few of these methods, along with an example.

Table 2.5. string Static Methods

Statement

Example

 void static  string.Format(    string format,    ...) 


[View full width]

string text, firstName, lastName; .... text = string.Format("Your full name is{0} {1}.", firstName, lastName); // Display //"Your full name is //<firstName> <lastName>." System.Console.WriteLine(text);


 void static string.Concat(     string str0,     string str1) 


 string text, firstName, lastName; ... text = string.Concat(firstName, lastName); // Display "<firstName><lastName>", notice // that there is no space between names. System.Console.WriteLine(text); 


 static int string.Compare(     string str0,     string str1) 


 string option; ... // String comparison in which // case matters. int result = string.Compare(option,"/help"); // Display: //     0 if equal //     negative if option < /help //     positive if option > /help System.Console.WriteLine(result); 


 

 string option; ... // Case-insensitive string comparison int result = string.Compare( option, "/Help", true); // Display: //     0 if equal //     < 0 if option < /help //     > 0 if option > /help System.Console.WriteLine(result); 



Table 2.6. string Instance Methods

Statement

Example

 bool StartsWith(     string value) bool EndsWith(     string value) 


 string lastName ... bool isPhd = lastName.EndsWith("Ph.D."); bool isDr = lastName.StartsWith("Dr."); 


 string ToLower() string ToUpper() 


 string severity = "warning"; // Display the severity in uppercase System.Console.WriteLine(    severity.ToUpper()); 


 string Trim() string Trim(...) string TrimEnd() string TrimStart() 


 // Remove any whitespace at the start // or end. username = username.Trim(); 


 string Replace(   string oldValue,   string newValue) 


 string filename; ... // Remove ?'s altogether from the string filename = filename.Replace("?", "");; 



Advanced Topic: C# Properties

Technically, the Length member referred to in the following section is not actually a method, as indicated by the fact that there are no parentheses following its call. Length is a property of string, and C# syntax allows access to a property as though it were a member variable (known in C# as a field). In other words, a property has the behavior of special methods called setters and getters, but the syntax for accessing that behavior is that of a field.

Examining the underlying CIL implementation of a property reveals that it compiles into two methods: set_<PropertyName> and get_<PropertyName>. Neither of these, however, is directly accessible from C# code, except through the C# property constructs.


String Length

To determine the length of a string you use a string member called Length. This particular member is called a read-only property. As such, it can't be set, nor does calling it require any parameters. Listing 2.13 demonstrates how to use the Length property, and Output 2.11 shows the results.

Listing 2.13. Using string's Length Member

 classPalindromeLength {   static void Main()   {       string palindrome;       System.Console.Write("Enter a palindrome: ");       palindrome = System.Console.ReadLine();       System.Console.WriteLine(           "The palindrome, \"{0}\" is {1} characters.",           palindrome, palindrome.Length);   } }

Output 2.11.

Enter a palindrome: Never odd or even The palindrome, "Never odd or even" is 17 characters.

The length for a string cannot be set directly; it is calculated from the number of characters in the string. Furthermore, the length of a string cannot change because a string is immutable.

Strings Are Immutable

The key characteristic of the string type is the fact that it is immutable. A string variable can be assigned an entirely new value, but for performance reasons, there is no facility for modifying the contents of a string. It is not possible, therefore, to convert a string to all uppercase letters. It is trivial to create a new string that is composed of an uppercase version of the old string, but the old string is not modified in the process. Consider Listing 2.14 as an example.

Listing 2.14. Error; string Is Immutable

classUppercase {   static void Main()   {       string text;       System.Console.Write("Enter text: ");       text = System.Console.ReadLine();       // UNEXPECTED: Does not convert text to uppercase                  text.ToUpper();                                                    System.Console.Write-Line(text);   } }

Output 2.12 shows the results of Listing 2.14.

Output 2.12.

Enter text: This is a test of the emergency broadcast system. This is a test of the emergency broadcast system.

At a glance, it would appear that text.ToUpper() should convert the characters within text to uppercase. However, strings are immutable and, therefore, text.ToUpper() will make no such modification. Instead, text.ToUpper() returns a new string that needs to be saved into a variable or passed to System.Console.WriteLine() directly. The corrected code is shown in Listing 2.15, and its output is shown in Output 2.13.

Listing 2.15. Working with Strings

classUppercase {   static void Main()   {       string text, uppercase;       System.Console.Write("Enter text: ");       text = System.Console.ReadLine();       // Return a new string in uppercase       uppercase = text.ToUpper();                                                 System.Console.WriteLine(uppercase);   } }

Output 2.13.

Enter text: This is a test of the emergency broadcast system. THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM.

If the immutability of a string is ignored, mistakes similar to those shown in Listing 2.14 can occur with other string methods as well.

To actually change the value in text, assign the value from ToUpper() back into text, as in the following:

text = text.ToUpper();


System.Text.StringBuilder

If considerable string modification is needed, such as when constructing a long string in multiple steps, you should use the data type System.Text.StringBuilder rather than string. System.Text.StringBuilder includes methods such as Append(), AppendFormat(), Insert(), Remove(), and Replace(), some of which also appear on string. The key difference, however, is that on System.Text.StringBuilder, these methods will modify the data in the variable, and will not simply return a new string.




Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

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