C# treats strings as if they were built-in types. C# strings are flexible, powerful, and easy to use.
In .NET, each string object is an immutable sequence of Unicode characters. In other words, methods that appear to change the string actually return a modified copy; the original string remains intact.
The declaration of the System.String class is:
public sealed class String :
IComparable, ICloneablee, IConvertible, IEnumerable
This declaration reveals that the class is sealed, meaning that it is not possible to derive from the String class. The class also implements four system interfaces — IComparable, ICloneable, IConvertible, and IEnumerable — which dictate functionality that System.String shares with other classes in the .NET Framework.
The IComparable interface is implemented by types that can be sorted. Strings, for example, can be alphabetized; any given string can be compared with another string to determine which should come first in an ordered list. IComparable classes implement the CompareTo() method.
ICloneable objects can create new instances with the same value as the original instance. In this case, it is possible to clone a string to produce a new string with the same values (characters) as the original. ICloneable classes implement the Clone() method.
IConvertible classes provide methods to facilitate conversion to other primitive types; these methods include ToInt32(), ToDouble(), and ToDecimal().
String Literals
The most common way to create a string is to assign a quoted string of characters, known as a string literal, to a user-defined variable of type string. The following code declares a string called newString that contains the phrase This is a string literal.
Verbatim Strings
Strings can also be created using verbatim string literals, which start with the (@) symbol. This tells the String constructor that the string should be used verbatim, even if it spans multiple lines or includes escape characters. In a verbatim string literal, backslashes and the characters that follow them are simply considered additional characters of the string.
Again, these declarations are interchangeable. Which one you use is a matter of convenience and personal style.
The ToString() Method
Another common way to create a string is to call the ToString() method on an object and assign the result to a string variable. All the built-in types override this method to simplify the task of converting a value (often a numeric value) to a string representation of that value.
C SHARP INTRODUCTION
C SHARP OUT LOOK
DOT NET AND C SHARP
C SHARP APPLICATION STRICTURE
OOPS INTRODUCTION
OOPS AND C SHARP
IDE AND C SHARP
INSTANTIATING OBJECTS IN C SHARP
CLASSES AND OBJECTS IN C SHARP
OPERATORS IN C SHARP
SWITCH AND ITERATION IN C SHARP
BRANCHING IN C SHARP
CONSTANTS AND STRING
No comments:
Post a Comment