C# Difference between String and string with Examples

  By : Suresh Dasari
  Posted On : 05-Jun-2023

Here, we will learn what is string and String in c#, differences between string and String in c#, and how to choose between string and String in c# with examples.

What are string and String in c#?

In c# string and String, both are useful to represent the sequential collection of characters. By using string and String, we can define and perform multiple operations on the string variables.

 

The String (uppercase S) is a class in the .NET framework, and it is available with System namespace. To use String type in applications, you need to import System namespace.

 

In c#, the string is just an alias for String class type (System.String), and you don’t need to import any namespace to use string (lowercase s) type in applications. Technically, there is no difference between string and String both will refer same data type while using it in c# applications.

 

To learn more about strings in c#, visit C# String with Examples.

C# String and string Example

Following is an example of using both string (lowercase s) and String (uppercase S) in c#.

 

using System;

namespace Tutlane
{
   class Program
   {
     static void Main(string[] args)
     {
        String s1 = "Welcome";
        string s2 = "Learn C#";
        Console.WriteLine("String (capital S) Type: " + s1.GetType());
        Console.WriteLine("string (small s) Type: " + s2.GetType());
        Console.ReadLine();
     }
   }
}

In this example, we defined two variables (s1, s2) using both string and String types. To use the String (capital S) class, we imported the System namespace in our example.

 

When you execute this example, you will get the following result.

 

String (capital S) Type: System.String
string (small s) Type: System.String

If you observe the result, both string and String are represent the same data type (System.String).

 

Generally, while writing the programs it is advisable to use string (lowercase s) type to define the variables and the String (uppercase S) to access the built-in string methods like String.Fromat(), String.IsNullOrEmpty(), etc. based on the requirements.

 

Following is an example of different ways of using string and String in c#.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         string s1 = "Welcome";
         string s2 = " to Tutlane";
         if (!String.IsNullOrEmpty(s1) && !String.IsNullOrEmpty(s2))
         {
            string result = String.Concat(s1, s2);
            Console.WriteLine(result);
         }
         Console.ReadLine();
      }
   }
}

In this example, we defined the variables (s1, s2) using string (lowercase s) and used String (uppercase S) to access the built-in string methods such as String.IsNullOrEmpty to verify whether the given string is null or empty and String.Contact method to join the given strings.

 

When you execute the above example, you will get the following result.

 

Welcome to Tutlane

As discussed, the string is an alias for System.String class, like this we have different alias names for different class types in c#. The following table lists some of the different alias data types that are available in c#.

 

Class TypeAlias
System.Int16 short
System.Int32 int
System.Int64 long
System.Single float
System.Double double
System.Decimal decimal
System.String string
System.Char char
System.Boolean bool
System.Byte byte
System.Object object
System.DateTime DateTime

Differences between String and string in C#

Following are the differences between string and String in c#.

 

  • Functionality wise both (string and String) are same. We use string keyword to define the variables and String type to access the built-in static methods such as String.Concat(), etc.
  • To use String class, you need to import System namespace but to use a string keyword you don’t require any namespaces.
  • The String class will contain different types of properties, methods, etc. but the string keyword is just an alias for System.String.
  • You can use a String class as a variable name but you are not allowed to use a string keyword as the variable name. To know more about using c# keywords as a variable name check this Keywords in C#.
  • While implementing the applications, you need to consider using the string keyword to define variables and use the String class when you want to perform actions using built-in static methods.