Best practice of use member of Primitive types

How I should write: long.MaxValue or Int64.MaxValue?

Are there standard from Microsoft?

13.10.2009 21:17:06
6 ОТВЕТОВ
РЕШЕНИЕ
long.MaxValue

because you are coding in c#.

Int64.MaxValue is a .NET type.

8
13.10.2009 21:18:43

Default StyleCop settings will tell you to use long.MaxValue.

4
13.10.2009 21:19:01

This is subjective, but I find Int64.MaxValue easier to read, because the capital letter makes it stand out clearly as a static member of a type called Int64, rather than an instance member of a local variable called long (not that there could ever be a local variable called long of course). But I don't think anyone reading your code is going to be confused whichever way you do it!

2
13.10.2009 21:26:35
Indeed: for example, I'd always declare a variable as long rather than Int64. But for some reason accessing members via the lower-case name doesn't come as naturally to me. Inconsistent? Yup, I guess so. grin
itowlson 13.10.2009 21:36:50

I would match it to the type of the variable it is being assigned to. If you are using long in the variable declaration, use long.MaxValue.

2
13.10.2009 21:29:47

Jeffrey Richter writes:

I prefer to use the FCL type names and completely avoid the primitive type names.

...

In C#, long maps to System.Int64, but in a different programming language, long could map to an Int16 or Int32. In fact, C++/CLI does in fact treat long as an Int32. Someone reading source code in one language could easily misinterpret the code's intention if he or she were used to programming in a different programming language. In fact, most languages won't even treat long as a keyword and won't compile code that uses it.

2
14.10.2009 06:09:53

I prefer the Int64.MaxValue. Also think about this case:

float  val = Single.Parse(..);
Single val = Single.Parse(..);

I do believe that the 2nd (FCL type name) is more clear than the 1st (built-in type name).

1
30.12.2010 07:14:23