Can anyone give me a REALLY good reason to use CLR type names instead of C# type names (aliases) in code (as a general practice)? [closed]
Asked Answered
M

7

15

We have a bit of a battle going on in our development team over this. I would love to hear what others think about this.

Mathew answered 11/6, 2009 at 14:27 Comment(2)
So what are the arguments being put forth in your development team battle?Intranuclear
The biggest argument for CLR types (in our team) is that everyone can understand what type is being used, regardless of the language selection. However, our team will never use anything BUT C#, so my argument was to not worry about that.Mathew
M
30

In the actual code? No, not unless you're going to have lots of people working on the code who are familiar with .NET but not with C#.

In member names? Absolutely. For instance, suppose Convert.ToSingle had been called Convert.ToFloat - that would be totally confusing for F# developers, for whom "float" means "64 bit floating point".

My general rule is C# aliases for implementation, CLR types for member names.

If you want to find some arguments in favour of using the CLR type names everywhere, Jeff Richter recommends that in "CLR via C#". (If you haven't already got it, buy a copy regardless of this matter - it's a wonderful book.) I didn't agree with the arguments he put forward, but there are some there, anyway.

Messinger answered 11/6, 2009 at 14:30 Comment(4)
Won't there be a very very very tiny performance hit at compile time when the compiler converts the aliases to type names? (In theory I mean, not that this is a real issue...)Sadness
Yeah I thought Jeff's justification in his book was a little extreme (pg 119 if anyone is interested). However I agree with him that they should of never included the aliases in the first place and that float val = br.ReadSingle(); is a little odd. Though I think Single val = br.ReadSingle() is also odd, because people have used the aliases as a de facto standard- I don't know anyone that uses Int32 over int.Bergstein
@antirysm: I suspect the aliases may actually be faster because the compiler doesn't need to check through the various namespaces and assemblies to find the relevant full name - it knows the fully qualified name already.Messinger
(But as you say, it won't be significant - probably not even measurable.)Messinger
G
3

I generally use the C# alias when declaring a variable, but the CLR types for the static members. I guess I just like the visual distinction it provides.

Grampus answered 11/6, 2009 at 14:33 Comment(0)
S
3

(Although it's not a particularily strong reason) I prefer type names over aliases because of IntelliSense standard coloring.

Sadness answered 11/6, 2009 at 14:58 Comment(0)
U
2

My previous development team adopted this practice because of the mix of C# and Visual Basic.NET developers. It was decided that the CLR types would make it easier for the C# and VB.NET people to communicate.

Unipersonal answered 11/6, 2009 at 14:34 Comment(0)
P
2

I think that almost the only time it makes sense to always use the CLR type names is in a mixed-language shop. One other possibility is if you are planning to switch from the current language to another one in the near future. In that case I would go with the CLR type names.

Other than that, there really isn't a strong motivating reason to choose one methodology over the other. It's far more important that you come to a consensus one way or another and make sure everyone is following the "standard".

Paradisiacal answered 11/6, 2009 at 14:41 Comment(0)
C
1

I think it makes sense to consistently use the CLR type names when calling static type member methods, as you have to do that on enums anyway. So for declaration, use the C# type names, but when calling static members, use the CLR types. This makes it easier to read and more consistent imho. Since, you can't write:

MyEnum value = enum.Parse(typeof(MyEnum), "value");

which would fit better with:

int i = int.Parse("1");
long l = long.parse("1");

You'd rather write:

int i = Int32.Parse("1");
long l = Int64.Parse("1");
MyEnum value = Enum.Parse(typeof(MyEnum), "value");
Cameliacamella answered 11/6, 2009 at 14:41 Comment(0)
A
0

Nope. I can't. It seems to me that the aliases are there to be used :)

Alysaalyse answered 11/6, 2009 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.