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

6

11

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 got :

public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>

{ ... }

It doesn't inherit from Object , does it mean that int is a primitive ?

Strathspey answered 20/10, 2015 at 22:19 Comment(3)
Its a struct, which is not allowed to specify a base type. The base type of System.ValueType is implied by the struct keyword. In other languages (e.g. Java) there is a distinction between objects and primitives, however in .NET the type system is unified. All non-pointer, non-interface types inherit from System.Object.Vauntcourier
Do you mean is Int32 "a data type provided by a programming language as a basic building block"? primativeGregoire
Wondering why it doesn't show Int32 inheriting Object since it doesDeckard
F
8

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.

Falcate answered 20/10, 2015 at 22:22 Comment(6)
This is not correct. int does not inherit from object. Assigning an int value to an object variable is called boxing.Colonnade
@YacoubMassad: Well, MSDN on System.Object says "All classes, structures, enumerations, and delegates" inherit from System.Object. System.Int32 is a structure. Draw conclusions for yourself.Midland
@YacoubMassad It does inherit from object - how would it override Equals, GetHashCode etc. otherwise? Boxing is related to representation, not inheritance.Mispickel
(1 is object) returns true. So I guess you guys are right.Colonnade
Well , tried this one and it was enough : int xx = 1; if (xx is object) { Console.Write("yeap"); }Strathspey
It's not as clear-cut as this answer makes out. You can assign an int to an object (object a = 12;) using boxing, however you cannot treat an int or other struct in the covariant position of an interface as an object (IEnumerable<object> b = Enumerable.Repeat(5,1); fails). So in that sense it does not always behave like an object.Seedbed
P
4

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

Plural answered 20/10, 2015 at 22:26 Comment(0)
G
3

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
Gustafson answered 20/10, 2015 at 22:23 Comment(4)
These types are built into C#, which is not necessarily the same as being built into .NET as a whole. Also, note that the linked page mentions "simple types", which is a C# thing that may or may not be meant to be the same as a "primitive type". Type.IsPrimitive provides a somewhat different list of types that are considered to be primitive types for the .NET Framework as a whole.Midland
There are no primitives in C#, you can call GetType on any int literal, and GetType is declared in System.ObjectFalcate
Try this code : int xx = 1; if (xx is object) { Console.Write("yeap"); }Strathspey
You should maybe consider a difference between how are primitives implemented in C# and Java. Java pretends to be fully objective language, and have type Integer, Double and so deriving from base object type. However these are reference types and are not the same as primitive int, double and so, and are not interchangable. In C# you always cast any primitive type to object or to it's nullable counterpart. This is due to superior boxing/unboxing features. Primitive types in C# are value types, and are stored on the stack as-is.Angellaangelle
F
1

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

Fishery answered 20/10, 2015 at 22:25 Comment(2)
I find this definition questionable as it lacks scope for the term "primitive types". Primitive types of what? The .NET Framework? Then, this definition cannot be correct, as different .NET languages can have different sets of keywords. A language? Then I wonder whether this is what was asked about.Midland
The definition of "Primitive type" you reference is for VB.NET.Midland
L
0

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.

Laurinelaurita answered 20/10, 2015 at 22:35 Comment(0)
G
0

int in C# is an alias for Int32, and they behave exactly the same. One would usually use Int32, instead of int, for readability and explicitness. Now, unfortunately your question can’t really be answered with a simple yes or no answer. - int implicitly derives from the ValueType Type which itself derives from the Object Type. And so, in C#, all Types do in-fact derive from the Object Type.

But, since ValueType can not be explicitly derived from (because it is an Abstract Class), the compiler needs to , and does, inherently know that int implicitly inherits from ValueType. So int does derive from the Object Type, just not explicitly.

Though int is not a Reference Type, it can still be treated like one ( like a Type that derives explicitly from the Object Type ) through a process called boxing.

It is a bit confusing at first. Everywhere online, and in books, people say that with regards to C#: All Types derive from the Object Type, which is true, but in the case of certain ValueType Types they just don’t inherit explicitly.

Furthermore, ints, as well as other ValueTypes, are constructed using Structs - not Classes in the way Reference types are.

Gaelan answered 7/6, 2022 at 5:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.