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.
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.
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
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.
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#.
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#.
Class Type | Alias |
---|---|
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 |
Following are the differences between string and String in c#.
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.String
class, you need to import System namespace but to use a string
keyword you don’t require any namespaces.String
class will contain different types of properties, methods, etc. but the string
keyword is just an alias for System.String
.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#.string
keyword to define variables and use the String
class when you want to perform actions using built-in static methods.