String Vs string in C# - CodAffection
[the_ad_placement id="before-content"]

String Vs string in C#

Difference between String and string

Introduction

In this post I would like to discuss about String Vs string in c#.It is a common interview question for c# beginners.

String vs string

String is a class in .Net library while string is a c# alias or keyword for String class.

String is a .Net Framework date type.Both String and string refers to the same class String in .Net Library. In a c# program you can use both String and string interchangeably. After compilation, both of them will be converted into System.String in IL(Intermediate Language).

string = System.String Class

String is a class in System namespace. so string is equivalent to System.String.

Consider the following code snippet

Above program will run without any problem. But if we remove the line for importing System namespace, declaration of str1  will create some problem like

System namespace not imported

That means we can’t use String Class in c# programs without importing System namespace but for string keyword we don’t need System namespace.

Best Practice

Actually both string and String refers same class in .Net Library, Even though, there are some recommended practice to use in c#programs.

use string keyword when declaring string variables.

use String if you need to refer specially to the class

Similar Aliases

C# alias for CLR datat types

objectSystem.Object
stringSystem.String
boolSystem.Boolean
byteSystem.Byte
sbyteSystem.SByte
shortSystem.Int16
ushortSystem.UInt16
intSystem.Int32
uintSystem.UInt32
longSystem.Int64
ulongSystem.UInt64
floatSystem.Single
doubleSystem.Double
decimalSystem.Decimal
charSystem.Char

1 thought on “String Vs string in C#”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
0 Shares
Share via
Copy link
Powered by Social Snap