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.
String, both are useful to represent the sequential collection of characters. By using
String, we can define and perform multiple operations on the string variables.
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
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 both will refer same data type while using it in c# applications.
To learn more about strings in c#, visit C# String with Examples.
Following is an example of using both
string (lowercase s) and
String (uppercase S) in c#.
In this example, we defined two variables (s1, s2) using both
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.
If you observe the result, both
String are represent the same data type (
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 in c#.
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.
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#.
Following are the differences between string and String in c#.
String) are same. We use
stringkeyword to define the variables and
Stringtype to access the built-in static methods such as String.Concat(), etc.
Stringclass, you need to import System namespace but to use a
stringkeyword you don’t require any namespaces.
Stringclass will contain different types of properties, methods, etc. but the
stringkeyword is just an alias for
Stringclass as a variable name but you are not allowed to use a
stringkeyword as the variable name. To know more about using c# keywords as a variable name check this Keywords in C#.
stringkeyword to define variables and use the
Stringclass when you want to perform actions using built-in static methods.