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
soriginally referred to - it just sets the value ofsto 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
nullkeyword in C# -charis 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, theEqualsmethod 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 typeobjectas far as the compiler is concerned, the normal==operator will be applied, and simple reference equality will be tested.
Related articles
Related discussion
-
Socket Programming in C# - Part 1
by graumanoz (23 replies)
-
Creating a Windows Service in VB.NET
by Templario55 (107 replies)
-
C# Windows Application Problem in Dynamically generated control
by BAmoozad (1 replies)
-
Using ADO.NET with SQL Server
by Manjot Bawa (23 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
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.
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
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
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.
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?
This thread is for discussions of Strings in .NET and C#.