C#/.NET: Difference between String and string
Question by Guest | Last update on 2021-01-13 | Created on 2016-08-04
When programming with C# it is possible to declare a string with lowercase as well as uppercase writing:
string s = "ABC"; String S = "ABC";
Both ways seem to work the same way in my code. Therefore, I wonder whether there is any difference at all between both declarations and if not I ask myself whether there are then two different string types available. In which situation should I use which variant?
Related Topics
PHP: Check Strings with Ctype-Functions for Character Classes
Article | 0 Comments
Delphi/Lazarus: Display current Date and Time
Tip | 0 Comments
Micro SD Memory Cards: Difference between microSD, microSDHC, microSDXC and microSDUC
Info | 12 Comments
Android Programming: Receive Responce from HTTP POST Request
Tutorial | 0 Comments
MySQL: Delete Data from Table - Difference between TRUNCATE, DELETE and DROP
Tutorial | 0 Comments
PHP: Current Date and Time
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
In C# "string" is an alias for "System.String". So, technically, both declarations are indeed identical, because internally, "string" is defined as "String".
Nevertheless, I recommend using the lowercase declaration always when you need a string object, for example:
However, if you need any class function, you should use the uppercase variant:
In this example, we are creating 3 string objects (a, b and c) using the lowercase string and we are using the class function String.Concat with the uppercase String in order to store the result into the object s.
Another argument for going this way is, that "string" is a consciously introduced lexical construct as an abstraction that should be used for a default string. Apart from that, "System.String" is only a type. So, if at any time in the future (unlikely) the default string type should be changed, string would not be the same as System.String anymore and if you already have used "string" you would be on the safe side because "string" would be changed to the new default string type at this time and you would not have to change your code.
By the way, the same thing also applies to int ( = System.Int32), long ( = System.Int6), bool ( = System.Boolean), char ( = System.Char), object ( = System.Object) and many other types.
Last update on 2021-01-13 | Created on 2016-08-05