Total Pageviews

Thursday, August 9, 2012

Strings vs string Builder

Defining Verbatim Strings

When you prefix a string literal with the
@ symbol, you have created what is termed a verbatim
string
. Using verbatim strings, you disable the processing of a literal’s escape characters and print
out a
string as is. This can be most useful when working with strings representing directory and
network paths. Therefore, rather than making use of
\\ escape characters, you can simply write the
following:
// The following string is printed verbatim
// thus, all escape characters are displayed.
Console.WriteLine(@"C:\MyApp\bin\Debug");

Strings: Strings are immutable , that is when you are manipulating the strings every time a new object is created, and the old one is garbage collected.
the
string type can be inefficient and result in bloated code if misused, especially when performing string concatenation. If you need to represent basic character data such as a US Social Security number (SSN), first or last names, or simple string literals used within your application, the string data type is the perfect choice.However, if you are building an application that makes heavy use of textual data (such as a word processing program), it would be a very bad idea to represent the word processing data using string types, as you will most certainly (and often indirectly) end up making unnecessary copies of string data. 

Strings use System.String Namespace.

String Builder;
What is unique about the
StringBuilder is that when you call members of this type, you are
directly modifying the object’s internal character data (and is thus more efficient), not obtaining a
copy of the data in a modified format. When you create an instance of the
StringBuilder, you cansupply the object’s initial startup values via one of many
constructors.
By default, a
StringBuilder is only able to hold a string of 16 characters or less;
however, this initial value can be changed via an additional constructor argument:
// Make a StringBuilder with an initial size of 256.
StringBuilder sb = new StringBuilder("**** Fantastic Games ****", 256);
If you append more characters than the specified limit, the
StringBuilder object will copy itsdata into a new instance and grow the buffer by the specified limit.

No comments:

Post a Comment