Why are there string and string versions of the string, and what should I use?

Well, that might be a stupid question, but I couldn't find any information on it.

Are String.Empty and string.Empty the same? I always find myself gravitating toward using an uppercase version (String.Empty), because I prefer the color and appearance of it in my IDE than the lowercase version (string.Empty) ...

Is there a โ€œrightโ€ way to use these that are different or completely dependent on personal preferences? It was my assumption that they are both the same, but to be honest, I never thought about it until for some reason today I thought, "If they both exist, they both must exist for some reason."

Is there a reason everyone knows about? If so, then what is it? Can anyone enlighten me?

PS "Exact duplicates" answer only half the question - "what is right?", But not "why do they both exist?"




Exact duplicate : What is the difference between a string and a string in C #?

Exact duplicate : String vs string in C #

+55
c #
Jan 16 '09 at 23:07
source share
5 answers

In C #, lowercase names are aliases for type names of System.xxx , for example. string is System.String and int is System.Int32 .

It is best to use these language aliases for type names, rather than their equivalent structure, for the sake of consistency. Therefore you are doing it wrong .; -)

For the reason that they both exist, .NET types exist because they are defined in a language-independent standard for .NET libraries called CTS (common type system). Why does C # determine that these aliases are outside of me (VB does something very similar). I think two reasons:

  • Habit. Ask all of these C and Java programmers to use C #, specifying the same type names for some basic types.
  • Laziness: you do not need to import the System namespace to use them.

EDIT . Since many people prefer other notation, let me point out that this is by no means unfounded. A good case can indeed be made to use names such as CTS and not C # keywords, and some other arguments presented at the superficial level are suggested in other answers. In terms of cleanliness / style, I probably agree.

However, consider whether it is worth violating a well-established contract that helps to unify code between projects.

+39
Jan 16 '09 at 23:09
source share

This conceptually looks like something like this:

 using int=System.Int32 
+11
Jan 16 '09 at 23:09
source share
Line

maps to the String AFAIK class, so they are the same.

The same is true for, for example, int and Int32.

+4
Jan 16 '09 at 23:10
source share

They are both the same.

Personally, I prefer to use the blue line using the C # keyword instead of the .NET class name for the same reason that I use int instead of Int32. In addition, lower case does not require the inclusion of the System ... namespace

+2
Jan 16 '09 at 23:11
source share

Personally, I prefer to use String, since both String and Object are references, while all other base types are value types. In my opinion, this is the clearest separation.

+2
Jan 17 '09 at 1:09
source share



All Articles