Difference between String and string in C#

When developing an application using C# , one would have noticed the usage of String and string class .

In the below example we use 2 string variable

string str1 = "Ginktage.com World";

String str2 = "Ginktage.com World"; 

What is the difference between String and string in C# ?

System.String is a .NET Framework type whereas string is simply an alias for the System.String class . They both compile to the similar code in IL (Intermediate Code), so at execution time there is no difference.

string cannot be used as a variable name since it is a reserved word unless we use it with the @ symbol. If one needs to declare a variable called String , there are possibilities to do it as shown below.

using System;

namespace GinktageConsoleApp
{
    internal class Program
    {
        private static Random randomNumber = new Random();

        private static void Main(string[] args)
        {
            string String = "This is a String variable";
            string string = "Invalid Variable Name";
            Console.ReadLine();
        }
    }
}

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply


x

Interesting BlogPosts to read

How to quickly remove multiple line breaks in Excel using VBA code?
There are times when you might copy few data from different source and you would end up getting unne...
How to change orientation of the worksheets in Microsoft Excel ?
This blog post will show the steps that are needed to change the orientation of the current workshee...
How to hide page breaks in worksheets in Microsoft Excel ?
When the page breaks is used to adjust the print area in a worksheet in excel , this would show up a...