1

Possible Duplicate:
What is the difference between String and string

I'm using string for a variable, like this

string myString = "test";

And I'm using String if I want to use some methods(?) of the Class String

String.Format...

I thought this is looking better. Buy some people are doing stuff like

String myString;
string.Format...

Its working. But I don't like this. How can I tell them to stop? Is there "C# rule" for stuff like this? The same thing for int,Int; char,Char; ...

Community
  • 1
  • 1
silla
  • 1,257
  • 4
  • 15
  • 32
  • @canon - Coding standards and consistency are important - for readability if nothing else. – Oded Oct 02 '12 at 22:42

3 Answers3

9

string is a C# alias to System.String.

If writing C#, you should be using the alias, so string.Format and string myString.

Both end up compiled to the same IL and mean the same thing, but C# has its idioms and using the type alias is part of them - in the same way that you would use int and not System.Int32.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Yes, this. Don't use `String` in C# code. I've seen people that use `String` when referring to static methods of the `String` class, and `string` everywhere else. I want to hurt those people. – cdhowie Oct 02 '12 at 22:42
  • Why do you say one should be using the alias if writing C#? Can you link a legit resource from Microsoft saying this is the preferred way? – Gromer Oct 02 '12 at 22:43
  • @Gromer - Why have an alias then? And you just need to look at any C# example on MSDN to see that only the aliases are used. – Oded Oct 02 '12 at 22:43
  • Having an alias doesn't mean one has to use it. Like I posted, I always use the alias, but I've never heard anyone say you should use them when writing C#. – Gromer Oct 02 '12 at 22:44
  • @Gromer After saying `using EventMap = Dictionary>;` do you then proceed to use `Dictionary>` in your code, or do you use `EventMap`? – cdhowie Oct 02 '12 at 22:45
  • I've already explained that I use the aliases. But for types, I see no reason to say one has to use the alias. Your example is comparing apples and oranges IMO. – Gromer Oct 02 '12 at 22:47
  • @Gromer I'm not comparing apples and oranges. `string` is an alias to `System.String`, and `EventMap` is an alias to `System.Collections.Generic.Dictionary>`. They are both another name for an existing type. One of them just happens to be imposed on you by the language. – cdhowie Oct 02 '12 at 22:48
  • So then why did Microsoft decide to _not_ use the type aliases in generated entities in EF if it is such a "bad" practice? – Gromer Oct 02 '12 at 22:50
2

Coding guidelines are an important thing if you are working on bigger projects and ensure that you can easily read code written by any of the developer in your team.

Unfortunately there isn't any official guideline on how you should format your C# code. Microsoft itself encountered the problem while developing the .NET framework and developed an internal set of style guidelines which grew into a full fledged program called StyleCop which has a default rule set with sensible settings.

According to these rules you should always use string instead of String:

string xyz;
string.Format();

The rules is the following:

SA1121 - UseBuiltInTypeAlias - Readability Rules

The code uses one of the basic C# types, but does not use the built-in alias for the type.

Rather than using the type name or the fully-qualified type name, the built-in aliases for these types should always be used: bool, byte, char, decimal, double, short, int, long, object, sbyte, float, string, ushort, uint, ulong.

A recommended reading is the history of StyleCop you can find here:

http://stylecop.codeplex.com/wikipage?title=A%20Brief%20History%20of%20CSharp%20Style&referringTitle=Documentation

It explains some of the problems you encounter with different people from different backgrounds working on the same code base and how they developed the rule set.

We recently implemented StyleCop in our own project and although it is a lot of work to really follow all the rules, the resulting code is much more readable. It also has a fairly good ReSharper integration which allows you to do many fixes automatically if you use ReSharper.

aKzenT
  • 7,775
  • 2
  • 36
  • 65
0

I usually express this as, "If there is an identical primitive type in the language, prefer it." In your specific case, which style to use is a preference rather than something that has major impacts to functionality. What is important is that whatever style is chosen is consistently applied by everyone in your team.

JamieSee
  • 12,696
  • 2
  • 31
  • 47