Home > c# > Difference between String and string

Difference between String and string

This is what MSDN has to say about this :

In C#, the string keyword is an alias for String. Therefore, String and string are equivalent, and you can use whichever naming convention you prefer.

So in other words string is equivalent to System.String ( data type defined in .net framework).

using System;

…………..

string s1 = “ABC”;
String s2 = “DEF”;

above line of code will compile and work as expected.

// using System;

…………..

string s1 = “ABC”;
String s2 = “DEF”;

but above line of code will produce compile time error as it can not identify the “String” data type.

So in short, (in c#) use either “string” or “System.String”. Functionally, using any of these doesn’t make any difference. The same explaination applies to other data types i.e. char & Char, object & Object etc.

——————————————————-

P.S. : I have heard that this same question being asked many time during interviews. I really don’t see any logic in testing the knowledge of candidate with such question. Programmer will never be able to see any difference in using “String” or “string” unless he/she has not added the directive “using System;” which is there by default when new class is created. But anyway if someone doesn’t know the difference this post should help them.

Advertisements
Categories: c# Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: