Initializing Strings


When you declare a variable of type string, the runtime doesn't automatically create a buffer to hold that string. Like other reference types in which the actual object isn't created until you use the new operator, string buffers aren't created until you initialize the string.

To declare and initialize a string variable:

  1. Type string str where str is the name of the variable to hold the string.

  2. Type = "My String" where "My String" is the string you wish to store in the string variable declared in step 1.

  3. Type ; (a semicolon) to end the statement ( Figure 4.8 ).

    Figure 4.8 Whenever you set a string variable to a literal value, you create a new string object in memory.
     string msg = "Your balance is 00"; 

graphics/tick.gif Tips

  • There are two ways of declaring string variables : You can use the C# keyword string or use the .NET Framework's class name System.String. Either way, you end up with the same thinga string object. Having two ways of declaring a string object could be confusing in C#, especially because C# usually adds a using System; statement to each file. That means that you can omit the word System when specifying System.String. If you do so, then the two ways of declaring strings would be string (lowercase s) and String (capital s). So which one should you use? The C# ECMA specification says to use the language-provided keyword. However, other experts in the field say that you should use the class name from the Framework. I'm sticking with the language's reserved word, for consistency ( Figure 4.9 ).

    Figure 4.9 The class name for strings is System.String. However, the language provides an alias called string (lowercase letters ). On top of that if you have a using statement for the System namespace you can also use String (capital S).
     //you say tomato  string  firstMsg = "Come here my son."; //you say to-mah-toe  System.String  secondMsg             = "Come here now!"; //if there is a //using System; statement  String  thirdMsg = "Nevermind"; 
  • Before you initialize a string variable (setting it equal to the contents of a string), the variable is equal to null. Figure 4.10 shows code that tests whether the value of the variable is null.

    Figure 4.10 The .NET Framework makes a distinction between uninitialized strings and empty strings. A string variable that hasn't been set to any literal value or to another string variable is null.
     string rat = "Splinter"; //rat declared                         //and created in                         //place. string mouse;           //mouse is null if (  mouse == null  )    mouse = "Mickey";     //string object                         //created and                         //address stored                         //in mouse string dog = "";        //this is                         //different from                         //null. in this                         //case a string                         //object is                         //created but                         //its contents                         //are empty. 
  • If you assign a different value to the string, the system silently creates a new string object ( Figure 4.11 ).

    Figure 4.11 Two girlfriends by definition can't share the same space. When you set the girlfriend variable to a different name, a new string object is created in memory and the variable points to the new object. The old string object is in memory until the system performs a garbage collection.
     string girlfriend = "Rebecca"; //a new string object is created //by the system silently  girlfriend = "Laurel";  
  • You can't break a string initialization statement into multiple lines unless you use literal strings (see "Using Literal Strings" later in this chapter) ( Figure 4.12 ).

    Figure 4.12 String literals can't be broken into two lines.
     string title1 = "The Grinch Who Stole Christmas";  string title2 = "Raiders of the Lost   Ark";  //this is illegal                         //you can't break a string                         //into multiple lines 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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