Strings

String are mainly used to store the text. It's a collection of characters enclosed within double quotes. In other words we can say that string is an array of characters.

Actually a string is an object which contains properties and methods, string properties will be used to do some operations on the string like converting string to uppar case, converting to lower case etc while methods can be used to get some useful info about the string like .length which will return length of the given string.

We can also combine multiple string using + operator or concat method. It will merge all given string and return final string. String is also used as a datatype to hold the string type of value into the variable.

let's see some example of the string, for example

Try it
string firstName = "Tutorials ";
string lastName = "Corner";

string fullName = firstName + lastName;
Console.WriteLine("Full Name is:" + fullName);

Above code will print the output as below

Try it
Full Name is:Tutorials Corner

We can also concat the multiple strings using .concat method of string object. .concat method accepts any number of string and concats all the string into one string. For example

Try it
string firstString = "Tutorials";
string secondString = "Corner";
string thirdString = "The expert tutor";
string fullString = string.Concat(firstString, secondString, thirdString);
Console.WriteLine("Full String is:" + fullString);

Above code will print the output as below

Try it
Full String is:TutorialsCornerThe expert tutor

It can hold maximum of 2GB or about 1 billion characters, but practically it depends on the CPU or memory of the perticular computer. Infact we can store very long text into the string. There are two ways to declare a string vaiable, using string keyword or String class, both will make no difference.

String Interpolation

We can also concat the string using string interpolation. We need to use placeholder to add the variable value into string and vaiable will be enclosed in curly braces. Stirng interpolation will always start with $ sign. For example

Try it
string firstName = "Tutorials";
string lastName = "Corner";
string fullName = $"Full name is {firstName} {lastName}";
Console.WriteLine(fullName);

Above code will print the output as below

Try it
Full name is Tutorials Corner

 

← Previous

Next →