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
-
Problem after strong naming an assembly
by rinkurathor1 (0 replies)
-
VB.net class to connect to sql database
by senol01 (2 replies)
-
ASP.NET Patterns every developer should know
by konikula (3 replies)
-
String was not recognized as a valid DateTime.
by buvanasubi (22 replies)
-
help me to get simple requirement
by Slicksim (1 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
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#.