Most applications require the use of chains in one form or another. Chains are required for the construction of SQL statements to access a database, for example. Knowing how to manipulate strings is useful in analyzing requests from Web clients and in building a response to the client.
Strings are reference types. You have been learning about the reference rates gradually in each chapter. The category of reference types include types defined with the keyword class. As you recall, you have to create an object before you can use a type variable. For example:
Account acct = new Account();
Before you create an object variable is null. The same rule applies to the chains. The string variables when declared are zero. However, what makes the ropes, they also have interesting features of value types (integers, longs, booleans, etc, for example). C # allows you to assign a string object without using the new operator, thus:
string sqlAuthors = "SELECT * FROM AUTHORS";
The previous code creates a new string object. Other features include how you can use the == and! = Operators to compare 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 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 chain, such as the length of the string. The second object that is created is a buffer to maintain the character of the string. The buffer is just a piece of memory to store the characters in the string. The string variable that point to the string declaration of external object, which in turn points in the character buffer.
What makes string types very interesting is that in. NET strings are immutable: the buffer can not be changed. Languages such as C # allow you to treat the string variable as a variable rate. For example, let you add another string to the string in the variable. However, this is an illusion provided by the compiler. Actually, when you change the contents of the string, the compiler of the question. NET runtime to create a brand-new string object.
String objects consume memory, and if you're not careful, may end up consuming too much memory. At some point that the memory must be recovered. . NET Framework uses a mechanism known as garbage collection to recover the memory.
At the end of this chapter you will learn about StringBuilder, which lets you create a string variable to a single character buffer that can be modified. When you are finished editing the buffer StringBuilder, StringBuilder you can ask to produce a string object from the results. Using StringBuilder will help you minimize the number of string objects that must be created, and this in turn, reduce the number of garbage collections that must occur to remove objects from memory.
0 comments:
Post a Comment