C# provides a type called a struct (short for structure) that is similar to a class. Although structs and classes are comparable in many ways, structs represent value types. Like classes, structs can have methods and properties, and can use the access modifiers public and private. Also, struct members are accessed via the member access operator (.).
The simple types are actually aliases for struct types. For instance, an int is defined by struct System.Int32, a long by System.Int64 and so on. All struct types derive from class ValueType, which in turn derives from object. Also, all struct types are implicitly sealed, so they do not support virtual or abstract methods, and their members cannot be declared protected or protected internal.
In this section, we present struct Char,[2] which is the struct for characters. Most Char methods are static, take at least one character argument and perform either a test or a manipulation on the character. We present several of these methods in the next example. Figure 16.15 demonstrates static methods that test characters to determine whether they are of a specific character type and static methods that perform case conversions on characters.
[2] Just as keyword string is an alias for class String, keyword char is an alias for struct Char. In this text, we use the term Char when calling a static method of struct Char and the term char elsewhere.
Figure 16.15. Char's static character-testing and case-conversion methods.
1 // Fig. 16.15: StaticCharMethods.cs 2 // Demonstrates static character testing methods 3 // from Char struct 4 using System; 5 using System.Windows.Forms; 6 7 public partial class StaticCharMethodsForm : Form 8 { 9 // default constructor 10 public StaticCharMethodsForm() 11 { 12 InitializeComponent(); 13 } // end constructor 14 15 // handle analyzeButton_Click 16 private void analyzeButton_Click( object sender, EventArgs e ) 17 { 18 // convert string entered to type char 19 char character = Convert.ToChar( inputTextBox.Text ); 20 string output; 2122 output = "is digit: " + 23 Char.IsDigit( character ) + " "; 24 output += "is letter: " + 25 Char.IsLetter( character ) + " "; 26 output += "is letter or digit: " + 27 Char.IsLetterOrDigit( character ) + " "; 28 output += "is lower case: " + 29 Char.IsLower( character ) + " "; 30 output += "is upper case: " + 31 Char.IsUpper( character ) + " "; 32 output += "to upper case: " + 33 Char.ToUpper( character ) + " "; 34 output += "to lower case: " + 35 Char.ToLower( character ) + " "; 36 output += "is punctuation: " + 37 Char.IsPunctuation( character ) + " "; 38 output += "is symbol: " + Char.IsSymbol( character ); 39 outputTextBox.Text = output; 40 } // end method analyzeButton_Click 41 } // end class StaticCharMethodsForm (a) (b) (c) (d) (e) |
This Windows application contains a prompt, a TextBox in which the user can input a character, a button that the user can press after entering a character and a second TextBox that displays the output of our analysis. When the user clicks the Analyze Character button, event handler analyzeButton_Click (lines 1640) is invoked. This event handler converts the input from a string to a char, using method Convert.ToChar (line 19).
Line 23 uses Char method IsDigit to determine whether character is defined as a digit. If so, the method returns true; otherwise, it returns false (note again that bool values are output capitalized). Line 25 uses Char method IsLetter to determine whether character character is a letter. Line 27 uses Char method IsLetterOrDigit to determine whether character character is a letter or a digit.
Line 29 uses Char method IsLower to determine whether character character is a lowercase letter. Line 31 uses Char method IsUpper to determine whether character character is an uppercase letter. Line 33 uses Char method ToUpper to convert character character to its uppercase equivalent. The method returns the converted character if the character has an uppercase equivalent; otherwise, the method returns its original argument. Line 35 uses Char method ToLower to convert character character to its lowercase equivalent. The method returns the converted character if the character has a lowercase equivalent; otherwise, the method returns its original argument.
Line 37 uses Char method IsPunctuation to determine whether character is a punctuation mark, such as "!", ":" or ")". Line 38 uses Char method IsSymbol to determine whether character character is a symbol, such as "+", "=" or "^".
Structure type Char also contains other methods not shown in this example. Many of the static methods are similarfor instance, IsWhiteSpace is used to determine whether a certain character is a whitespace character (e.g., newline, tab or space). The struct also contains several public instance methods; many of these, such as methods ToString and Equals, are methods that we have seen before in other classes. This group includes method CompareTo, which is used to compare two character values with one another.
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index