string type in C#.NET

First of all String is of reference type so, it is stored on the heap rather than on a stack. So whenever you assign a string variable to another string variable then it means you are creating two references of same string.

Lets take a look on following example:

/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at :*/

// File name : stringCS.cs

using System;

namespace CSharp.AStepAhead.stringCS

class stringCS
static void Main()
string str = "This is example shows the various string features ";
string str1 = "This is first String";
string str2 = "This is second String";
string str3;
Console.WriteLine("{0}\n", str);
Console.WriteLine("This is string str1 :{0}\n", str1);
Console.WriteLine("This is string str2 :{0}\n", str2);
str3 = str1 + str2;
Console.WriteLine("String Concatenation : {0}", str3);
Console.WriteLine("str1 has {0}characters", str1.Length);
str3 = "ESS";
string res;

if (str1.Contains(str) == true)
res = "Yes";
res = "No";
Console.WriteLine("Does str1 contains \'{0}\':{1}", str,res);
Console.WriteLine("Str2 in upper case {0}", str2.ToUpper());
Console.WriteLine("Str1 in lower case {0}", str1.ToLower());
Console.WriteLine("In Str1 character \'{0}\' is replaced by character \'{1}\' : \nnow str1 = {2}", str,str3, str1.Replace(str, str3));


Hope the above tiny article elaborate the Title.


Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 149 articles authored by Gaurav Aroraa


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: