Built-in string type in C#

Built-in string type in C#

In the previous tutorial, We learnt about some of the built-in types like int,float,boolean etc.

In this tutorial,We are going to learn about Built-in string type in C#, Escape sequences in C# and Verbatim Literal.

Strings in C#

In C#, like other programming languages, Strings are placed inside double quotes i.e. Any character or word or number placed inside of double quotes is a string

eg: string name =”Geek”
eg: string num =”9″
eg: string char =”c”

If we want to know the variable’s data type, we can use C#’s inbuilt method called GetType();
eg: int num =9
eg: string number =”9″
Console.WriteLine(num.GetType()) //Returns System.Int(Int type)
Console.WriteLine(number.GetType()) //Returns System.String(String type)

In the above example, 9 is placed inside of double quotes. So, on calling the GetType() method, it is returning String Type

Escape Sequence in C#

If we want to print non-printable characters in a string, we have to make use of escape sequence characters.Suppose we want the output to be in double quotes
and in a string variable, if we declare a variable with four double quotes in it
string name= “”Geek”” //Compiler error
There are some non-printable characters. We cannot print these characters in a string.So we make use of escape sequence to print those characters.
eg: string name =”\”Geek” //Returns “Geek
name =”\”Geek\”” //Returns “Geek”

There are some non-printable characters in C#.You can find more at this link

Verbatim Literal in C#

MSDN says that a verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character.A simple example is @”geek”

eg: string name =”\”Geek\”” //Returns Geek
name = @”\”Geek\”” //Returns \”Geek\”
It doesnt care whether it is an escape sequence or another character. It simply
stores whatever the value in the double quotes

Leave a Reply

Your email address will not be published. Required fields are marked *