Difference between string and String in C#

In this article, we will learn about what is the difference between string and String in C#.

In C#, string is an alias for the String class in .NET.

In fact, every C# type has an equivalent in .NET. As another example, short and int in C# map to Int16 and Int32  in .NET.

In C#, string is a class. Both string and String class comes from the System.String class.

The small difference between string and String is, to use String class, we need to import the System namespace on top of our file. For string keyword, we don’t have to do this.

That is, “Many developers prefer to declare a string variable with string but use the String class when accessing one of its static members”

So, technically there is no difference between string and String.

Example:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstName = "Joseph";

            string lastName = "Prince";

            string fullName = String.Concat(firstName, lastName);

            Console.WriteLine(fullName);
        } 
    }
}

If you like this article, please subscribe my blog to get C#, Angular, Programming, Web Development tutorials at your inbox

Processing…
Success! You're on the list.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s