Can anyone give me a REALLY good reason to use CLR

2019-03-11 04:20发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

Interesting: The compiler doesn’t allow you to type enum using the “Int32” but it does when you use “int”.



回答4:

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



回答5:

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.



回答6:

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".



回答7:

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");


回答8:

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



标签: c# clr