Characters, Escape sequences, Strings, Concatenations and Conversions in C#

Posted by in C# category on for Beginner level | Points: 250 | Views : 6295 red flag

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.


So far, we have seen operators in C#, and let's see the usage of char's and strings in C# programming.


The main objective of this article is to learn about character's and strings in C# programming.


'char' is the keyword used to represent a Unicode character. .Net Framework type of char is System.Char. It is a 16-bit numeric value (means it occupies 2 bytes).
char literal is specified in single quotes.
char c = 'c';
Escape Sequences:
Escape sequences are the characters that cannot be interpreted or expressed literally.
Let's have a simple code that explains about char literals in detail,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
    class Program
        static void Main()
            char s = 'S';                 // Simple Character
            char nextLine = '\n';         // Escape Sequence for going to the next line 
            char back = '\r';             // Escape Sequence for carriage return

            Console.WriteLine(nextLine);  // Prints nothing but skips to the next line.
            Console.WriteLine(s);         // Prints the character 'S'
In the above code, we are using different types of characters used in C#.

Note: please read comments in the code.

Now press Ctrl + F5 and see the following output in your Console,

Below table shows the list of Escape sequence characters.
\'Single quote0x0027
\"Double quote0x0022
\fForm feed0x000C
\nNew line0x000A
\rCarriage return0x000D
\tHorizontal tab0x0009
\vVertical tab0x000B

char can be converted implicitly to int, uint, long, ulong, ushort, float, double or decimal. And there are no implicit conversions from other types (explicit conversion is required). 

System.Char consists of several methods that works with char values.


  • string is an immutable object which represents a series of Unicode characters. 
  • It can be used in many methods without its data change. 
  • It is a valid which reduces copies. 
  • It makes the programs more robust.
  • .Net Framework type is 'System.String'.
string literal is specified in the double quotes.
string s = "Hello !!";
What is Immutability?
  • 'Immutable' means they cannot be changed after they have been created.
  • But if we go with the below example, we can say that, the above meaning is not exactly true in real programming,
    string s = "Hello";
    s = "Bye";
    In the above code we are changing the value of the string which gives the following output,

  • Hence, the 'immutability' means where the String reference is pointing to. In the above example, initially s is pointing to the string "Hello" and later it has been changed (pointed) to "Bye". 

'String' is the class which provides so many methods creatingcomparing and manipulating the strings.

Let's have a simple code that explains strings well,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
    class Program
        static void Main()
            string s1 = "Hello";
            string s2 = "Hello";
            Console.WriteLine(s1 == s2);       // Prints True as it follows the Valu-type approach

            string s3 = "Escape Sequence:\n";
            Console.WriteLine(s3);             // Prints Escape Sequence and goes to the next line    
In the above code, we are comparing strings and also used escape sequences.

Note: please read comments in the code.

The output of the above code will be,

String Concatenation:
To concatenate (combine) two strings, we have to use '+' operator.
string s = "Abhi" + "ram"   // result becomes Abhiram
We can also add a non-string value like,
String s = "a" + 6  // a6
String Comparisions:
string doesn't supports <, > operators for comparisons. We have to use CompareTo method to compare the strings. 


In this article, we have discussed Characters and Strings in C# with examples. Hope you understand.

Thanks for reading.


Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)