Is int (Int32) considered an object in .NET or a primitive (not int?)?

前端 未结 6 1818
小蘑菇
小蘑菇 2020-12-31 20:19

Is int (aka Int32) an object , or a primitive in .NET (I\'m not asking regarding int?)?

I hit F12 on the saved word int and g

6条回答
  •  甜味超标
    2020-12-31 20:31

    MSDN - "The primitive types are identified through keywords, which are aliases for predefined types in the System namespace. A primitive type is completely indistinguishable from the type it aliases: writing the reserved word int is exactly the same as writing System.Int32. Because a primitive type aliases a regular type, every primitive type has members. For example, Integer has the members declared in System.Int32. Literals can be treated as instances of their corresponding types."

    So basically int and Int32 are synonymous; You would be inclined to use int where you just need 'an integer',where when using Int32 the size is explicitly shown so future maintainers will know it's safe to enlarge an int if appropriate, but should take care changing Int32 variables in the same way. The resulting code using both will be identical, but the difference is only in the readability of the code or also if you want to call it code presentation.

    You can read Applied .NET Framework Programming - the author Jeffrey Richter makes a good example of using the full type names. Here are the main things that I remembered:

    Type names can vary between .NET languages. For example, in C#, long maps to System.Int64 while in C++ with managed extensions, long maps to Int32. Since languages can be mixed-and-matched while using .NET, you can be sure that using the explicit class name will always be clearer, no matter the reader's preferred language.

提交回复
热议问题