Braintique.com header
Left Navigation Bar

Understanding C# Strings

In C#, a text string is stored in a data type named string, which is an alias to the System.String type. In other words, when you create a string, you instantiate a System.String object.

In addition to its instance members, the System.String class has quite a few important static methods. These methods don’t require a string instance to work.

It’s important to understand that the string type is immutable. This means that once it has been created, a string cannot be changed. No characters can be added or removed from it, nor can its length be changed.

But wait, you say, my strings change all the time when I do things like

string str = "I am going to have a lot of fun";
str += " with Dudley this summer. ... ";
str += " The End";

Well, my friend, what this set of statements actually does is create a new instance of the variable str each time a value is assigned to str. In other words, in this example, an instance of str has been created—and a literal string value assigned to it—three times. The statement that the type is immutable means that an instance cannot be edited—but it can be assigned.

Using an instance method without an assignment is syntactically legal, but it doesn’t do anything (has no effect). For example

string str = "hello";
str.Replace ("l", "L"); // Doesn’t do anything

does not change the contents of the variable str, although combining it with an assignment would work:

string str = "hello";
str = str.Replace ("l", "L");

The value of the “new,” assigned str is, of course, “heLLo”.

Is there any real problem with this? Well, no, it’s easy enough to use assignments when you need to change the value of string. But instantiating and destroying strings every time you need to change or edit one has negative performance consequences. If performance is an issue, you should consider working with instances of the System.Text.StringBuilder class, which are mutable (can be changed). The StringBuilder class is discussed later in this article in a little more detail.

Once created, a string has a specific length—which, of course, is fixed (since the type is immutable). A C# string is not a terminated array of characters (unlike in the C language, in which a string is simply an array of characters terminated with a zero byte). So, how does one determine the length of a C# string? As you probably know, you can query the read-only Length property of the string instance to find its length.

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write