Chapter 4. Strings


Most applications require the use of strings in one way or another. Strings are required for building SQL statements when accessing a database, for example. Knowing how to manipulate strings is useful in analyzing requests from Web clients and in constructing a response for the client.

Strings are reference types. You've been learning about reference types little by little in each chapter. The category of reference types includes types you define with the class keyword. As you may recall, you have to create an object before you can use a variable of the type. For example:

 Account acct = new Account(); 

Before you create an object, the variable holds the value of null . The same rule holds true for strings. String variables when declared are equal to null . However, what makes strings interesting is that they also have characteristics of value types (integers, longs, Booleans, etc., for example). C# lets you allocate a string object without using the new operator, like this:

 string sqlAuthors = "SELECT * FROM   AUTHORS"; 

The above code creates a new string object. Other characteristics include the way that you can use the == and the != operators for comparing two string objects.

So what is a string? Internally you can think of a string as an array of characters . When you create a string object, in essence two objects are being created in one. The first object is an instance of the string class. The string class has members that describe the physical attributes of the string, such as the length of the string. The second object that gets created is a buffer to hold the characters of the string. The buffer is just a chunk of memory where the characters of the string are stored. The string variable you declare will point to the outer string object, which in turn points to the buffer of characters.

What makes string types really interesting is that in .NET strings are immutable ”the contents of the buffer can't be changed. Languages like C# enable you to treat your string variable as a changeable type. For example, they let you append another string to the string in your variable. However, this is an illusion provided by the compiler. In reality, when you change the contents of your string, the compiler asks the .NET runtime to create a brand-new string object.

String objects consume memory, and if you're not careful, you can end up consuming too much memory. At some point that memory needs to be reclaimed. The .NET framework uses a mechanism known as garbage collection to reclaim the memory.

At the end of this chapter you will learn about StringBuilder , which lets you create a changeable string ”a single buffer of characters that can be modified. When you're done modifying the StringBuilder buffer, you can ask StringBuilder to produce a single string object from the result. Using StringBuilder will help you minimize the number of string objects that need to be created, and this will in turn decrease the number of garbage collections that need to occur to delete those objects from memory.



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