String in C#

String is a C# data type which stores text value. This type stores a collection of characters clubbed together to form a text. Basic syntax to create a string variable in C# is

string strWelcome = "Welcome to CheezyCode";




string OR String


Both string and String types are same because string is an alias for String type. The c# compiler treats string as an alias which somewhat may look like using string = System.String; C# .Net has some predefined type alias like int for System.Int32, long for System.Int64 etc. So feel free to use any thing, be it string or String. Both will work exactly the same.


Immutable String


This is the most common thing asked about strings. Immutable in programming world means something that is unchangeable. String type is Immutable which means that the value of string can't be changed directly. A new string is created in memory whenever string is altered. Have a look at below example:

string x = "Test Value";   //A new string for which some memory is allocated 10 characters.
x = x + "Another Value";   //A new memory location is allocated for 10 + 13 ~ 23 characters

Because of this reason, we always assign the altered string back to variable name, as shown x + "Another Value" is assigned back to x. So x is recreated and stored with the changed value at new memory location.

String.Concat


String.Concat method provides a clean way to concatenate different strings. Suppose you want to concatenate different strings into a single string variable. Now one way is to use '+' operator but String.Concat provides a clean and more readable approach. Consider below example

string x = "This" + " is" + " a" + " concatenated " + " string."
string x = String.Concat("This", " is", " a", " concatenated ", " string.");

Both these lines compile to same code so any approach can be used to append strings.

Escape Sequences In String


.Net provides certain sequences to be used within string value called as Escape Sequences. These sequences start with a backlash '\' followed by a character or number like '\r' or '\n'. String understand these Escape Sequences for specific interpretations. You can use these to show tab spaces or newline in your strings. Shown below a tabular mapping to different escape sequences used in .Net.

Escape Sequences In String C#


Verbatim String In C#


In C# Verbatim string are those which don't process the escape characters and are most commonly used to describe fully qualified file name. Any string which has a '@' character in start becomes verbatim string. Shown below example of verbatim string

string path = @"C:\Documents\File.txt";

The compiler throws an error if the escaped sequences used in a string are not understood. So when we need compiler to ignore the escape sequences and want to use string as it is, always use append '@' character.

Let us know if you have any query. Happy Reading.(refer full series here - C# Tutorial Index)



Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example