Information Technology Reference
In-Depth Information
Miscellaneous Topics
In this chapter, you will look at a number of other topics that are important in using C#, but
that don't fit neatly into one of the other chapters. These include string handling, nullable
types, the Main method, documentation comments, and nested types.
Strings
Zeros and ones are fine for internal computation, but for human readable input and output, we
need strings of characters. The BCL provides a number of classes that make string handling easy.
The C# predefined type string represents the .NET class System.String . The most impor-
tant things to know about string s are the following:
￿ string s are arrays of Unicode characters.
￿ string s are immutable—they cannot be changed.
The string type has many useful string manipulation members. These allow you to deter-
mine the length of a string, concatenate strings, change the case of a string, and many other
useful tasks. Some of the most useful members are listed in Table 23-1.
Table 23-1. Useful Members of the string Type
Member
Type
Meaning
Length
Property
Returns the length of the string
Concat
Static method
Returns a string that is the concatenation of its argument strings
Contains
Returns a bool value indicating whether the argument is a sub-
string of the object string
Method
Format
Static method
Returns a formatted string
Insert
Method
Inserts a string at a specific point in the object string
Remove
Method
Removes a set of characters from the object string
Replace
Method
Replaces a character or string in the object string
SubString
Method
Retrieves a substring from the object string
ToUpper
Method
Returns a copy of the object string where the alphabetic charac-
ters are all uppercase
ToLower
Method
Returns a copy of the object string where the alphabetic charac-
ters are all lowercase
The names of many of the methods in Table 23-1 sound as if they are changing the string
object. Actually, they are not changing the strings but returning new copies. For a string , any
“change” allocates a new immutable string.
Search WWH ::




Custom Search