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
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.
Look at the C# reference source or from here: https://msdn.microsoft.com/en-us/library/system.int32.aspx#
The primitive types are the one identified through keywords, so yes int is a primitive type.
The primitive types also allow you to use that as literals.
However, the underlying type that the keyword identifies is System.Int32 which is not a primitive types. This is a value type, not a reference type (or object).
Referring to this MSDN site there are 15 build in types, from which 2 are classes (object and string) and the rest are primitives:
bool - System.Boolean
byte - System.Byte
sbyte - System.SByte
char - System.Char
decimal - System.Decimal
double - System.Double
float - System.Single
int - System.Int32
uint - System.UInt32
long - System.Int64
ulong - System.UInt64
object - System.Object
short - System.Int16
ushort - System.UInt16
string - System.String
Int32
is a struct, which is like a type (compile time) and not an object (run time). So you can't say "Int32
is an object", but you could say "Int32
inherits from object".
A struct is a ValueType and a ValueType derives from object.
int
and Int32
and synonyms where Int32
is better suited in operations where the reader cares about the length in bits (bit fiddling operations, overflow situations etc.)
Everything in C# inherits from object
, including int
.
From msdn:
Int32 is an immutable value type that represents signed integers
and
Both reference and value types are derived from the ultimate base class Object.