1. Home
  2. Computing & Technology
  3. C / C++ / C#

C# Programming Tutorial - About Strings and Chars

By , About.com Guide

3 of 10

More about Strings and Chars in C# Programming

Strings and Characters

In C#, characters are 16 bit Unicode characters. This means that the old method of one byte = one character no longer holds. This is quiet a change if you're used to the old way of treating characters and bytes as more or less interchangeable. They are the same size as 16 bit ints - (i.e. shorts or ushorts) but the compiler will complain if you try to use them directly. That's what type-strictness is all about.

Characters are declared inside single quotes e.g. char z='Z'. Remember it's double quotes for strings "like this", single quotes for 'a' char.

Declaring Special Characters C# continues the tradition (in C, C++ etc) of using backslashes \ to escape special characters. Because " is used to delimit strings (and ' for individual characters) if you wish to include a single quote in a string you have to use \' and \" for a double quote. File paths have the problem that to include a backslash you also have to double it e.g . "c:\\folder\\folder2\\file.txt". Other characters that get the same treatment include a terminating zero (\0), Linefeed \n, Carriage Return \r and tab \t plus a few others.

This list shows the defined escape sequences

  • Sequence - Value - Name
  • \' 0x0027 Single quote
  • \" 0x0022 Double quote
  • \\ 0x005C Backslash
  • \0 0x0000 Null
  • \a 0x0007 Alert
  • \b 0x0008 Backspace
  • \f 0x000C Form feed
  • \n 0x000A New line
  • \r 0x000D Carriage return
  • \t 0x0009 Horizontal tab
  • \U \Unnnnnnnn Unicode escape sequence for surrogate pairs.
  • \u \u0043 Unicode escape sequence ( "C" )
  • \v Vertical tab 0x000B
  • \x \x0044 Unicode escape sequence similar to "\u" except with variable length. ( "D" )

On the next page Still more about Strings and Chars

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. More about Strings and Chars in C# Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.