Library tutorials & articles

Strings in .NET and C#

Introduction

The System.String type (shorthand string in C#) is one of the most important types in .NET, and unfortunately it's much misunderstood. This article attempts to deal with some of the basics of the type.

What is a string?

A string is basically a sequence of characters. Each character is a Unicode character in the range U+0000 to U+FFFF (more on that later). The string type (I'll use the C# shorthand rather than putting System.String each time) has the following characteristics:

It is a reference type
It's a common misconception that string is a value type. That's because its immutability (see next point) makes it act sort of like a value type. It actually acts like a normal reference type. See my articles on parameter passing and memory for more details of the differences between value types and reference types.
It's immutable
You can never actually change the contents of a string, at least with safe code which doesn't use reflection. Because of this, you often end up changing the value of a string variable. For instance, the code s = s.Replace ("foo", "bar"); doesn't change the contents of the string that s originally referred to - it just sets the value of s to a new string, which is a copy of the old string but with "foo" replaced by "bar".
It can contain nulls
C programmers are used to strings being sequences of characters ending in '\0', the nul or null character. (I'll use "null" because that's what the Unicode code chart calls it in the detail; don't get it confused with the null keyword in C# - char is a value type, so can't be a null reference!) In .NET, strings can contain null characters with no problems at all as far as the string methods themselves are concerned. However, other classes (for instance many of the Windows Forms ones) may well think that the string finishes at the first null character - if your string ever appears to be truncated oddly, that could be the problem.
It overloads the == operator
When the == operator is used to compare two strings, the Equals method is called, which checks for the equality of the contents of the strings rather than the references themselves. For instance, "hello".Substring(0, 4)=="hell" is true, even though the references on the two sides of the operator are different (they refer to two different string objects, which both contain the same character sequence). Note that operator overloading only works here if both sides of the operator are string expressions at compile time - operators aren't applied polymorphically. If either side of the operator is of type object as far as the compiler is concerned, the normal == operator will be applied, and simple reference equality will be tested.

Comments

  1. 05 Jan 2006 at 21:12

    Quote:
    [1]Posted by eliassal on 10 Nov 2005 06:17 AM[/1]
    Hi, i read 2 of your articles they are interesting. However, I can not figure out what or where the contents come from for the variable
    readonly string[] LowNames and how it is used. I would appreciate a short description.


    Do you mean you don't know how the array is populated, or you don't know why I populated it with the names I did?


    To answer the first question - if you look at the code, you'll see there's a static field initializer:


    static readonly string[] LowNames =
    {
       "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
       "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
       "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
       "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"
    };


    The names come from the man page for ASCII on a unix box


    Jon

  2. 05 Jan 2006 at 21:11

    Quote:
    [1]Posted by av_rocksu on 11 Sep 2005 06:00 AM[/1]
    I'm working on project which converts differnt forms of temperature like Celcius, Farenheit and kelvin etc..
    I also have to put up access keys to it for each conversion. Please help me with conversion function and access keys. I also have to put up access keys for reset as well as exit buttons on the form. What is is double type variable?


    I'm not at all sure what this has to do with Strings, but the C# type for double precision binary floating point values is "double".


    See my article on floating point arithmetic for more information. It's at http://www.pobox.com/~skeet/csharp/floatingpoint.html
    (Sorry, the "insert link" button doesn't seem to work in Firefox.)


    Jon

  3. 10 Nov 2005 at 06:17

    Hi, i read 2 of your articles they are interesting. However, I can not figure out what or where the contents come from for the variable
    readonly string[] LowNames and how it is used. I would appreciate a short description.

  4. 11 Sep 2005 at 06:00

    I'm working on project which converts differnt forms of temperature like Celcius, Farenheit and kelvin etc..
    I also have to put up access keys to it for each conversion. Please help me with conversion function and access keys. I also have to put up access keys for reset as well as exit buttons on the form. What is is double type variable?

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Strings in .NET and C#.

Leave a comment

Sign in or Join us (it's free).

Jon Skeet C# MVP currently living in Reading and working for Google.
AddThis

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

We'd love to hear what you think! Submit ideas or give us feedback