Is "int" a primitive type or an object??

M

Matt

I want to know if "int" is a primitive type, or an object?

For example, the following two approaches yield the same result.
int t1 = int.Parse(TextBox2.Text); //method 1
int t2 = System.Int32.Parse(TextBox2.Text); //method 2

And people said "int" is a C# alias for "System.Int32". If this is the case,
can we say "int" is an object??

Because methods should operate on object, not on primitive type.

object1.method(...); //make sense
primitiveType.method(); //doesn't make sense ??

Please advise. Thanks!
 
R

Rob Teixeira [MVP]

Int is a primative type.
However, the runtime can "box" a primative so it can operate as an Object,
thereby keeping the paradigm that everything can be treated as having the
object base type.
Note that value types can indeed have methods, and static methods (like
Parse) don't require an instance handle, so boxing isn't required in that
particular case. Look up "value type" and "boxing" in the MSDN to get more
info.
So, in short, int (which is really just int32 in C#) isn't an object, but it
can be treated as an object.

-Rob Teixeira [MVP]
 
S

Simon Smith

On Sat, 3 Jan 2004 18:38:22 -0800 in article
<[email protected]> in
microsoft.public.dotnet.languages.csharp , "Matt"
I want to know if "int" is a primitive type, or an object?

For example, the following two approaches yield the same result.


And people said "int" is a C# alias for "System.Int32". If this is the case,
can we say "int" is an object??

Because methods should operate on object, not on primitive type.

object1.method(...); //make sense
primitiveType.method(); //doesn't make sense ??

Please advise. Thanks!

The C# compiler translates all references to 'int' to 'System.Int32'.
Thus any use of 'int' is exactly the same as using System.Int32 - no
difference whatsoever. Why use int, then, apart from a bit less
typing? On 64-bit systems it will translate to 'System.Int64' - i.e.
use the appropriate size for the machine it's running on.

In C# (in IL generally) there is no such thing as a 'primitive type'.
Everything is a class or an object. There are two types of classes -
Value types and Reference types. Value types are like primitive types
- more so than reference types anyway - but the importasnt thing to
remember is that there are NO 'primitive' types in the sense that that
is usually used.

int (that is, System.Int32) is not an object. It is a class. As a
class it can - and in fact does - have static methods which is what
..Parse(..) is.

Once again, forget primitive types! They don't exist!
 
J

Jon Skeet [C# MVP]

Simon Smith said:
The C# compiler translates all references to 'int' to 'System.Int32'.
Thus any use of 'int' is exactly the same as using System.Int32 - no
difference whatsoever. Why use int, then, apart from a bit less
typing? On 64-bit systems it will translate to 'System.Int64' - i.e.
use the appropriate size for the machine it's running on.

No it won't. In C#, int is *always* defined as a shorthand for Int32,
thank goodness.
 
J

Jon Jagger

Simon Smith said:
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<[email protected]> in
microsoft.public.dotnet.languages.csharp , "Matt"


The C# compiler translates all references to 'int' to 'System.Int32'.
Thus any use of 'int' is exactly the same as using System.Int32 - no
difference whatsoever. Why use int, then, apart from a bit less
typing? On 64-bit systems it will translate to 'System.Int64' - i.e.
use the appropriate size for the machine it's running on.

I don't think this is true. In C# int is the same as System.Int32. It
doesn't matter what you're running on. There are BCL types that correspond
to natural word size of the machine but they aren't called int.
In C# (in IL generally) there is no such thing as a 'primitive type'.
Everything is a class or an object. There are two types of classes -
Value types and Reference types. Value types are like primitive types
- more so than reference types anyway - but the importasnt thing to
remember is that there are NO 'primitive' types in the sense that that
is usually used.

int (that is, System.Int32) is not an object. It is a class. As a
class it can - and in fact does - have static methods which is what
.Parse(..) is.

Once again, forget primitive types! They don't exist!

I would argue that they do and they don't. There are mappings to CIL but
when your programming in C# you are programming in C# and not in CIL. From
that point of view I would say that C# does have "primitive types" or more
accurately (as defined by the ECMA standard) it has simple types (11.1.3).
It all depends on your perspective.
HTH
Cheers
Jon Jagger
 
T

Thomas Tomiczek [MVP]

Rob Teixeira said:
Int is a primative type.

No.

There is no such thing as a "primitive type" in the CLS:

Int is a STRUCT. System.Int32.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
T

Thomas Tomiczek [MVP]

Simon Smith said:
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<[email protected]> in
microsoft.public.dotnet.languages.csharp , "Matt"


The C# compiler translates all references to 'int' to 'System.Int32'.
Thus any use of 'int' is exactly the same as using System.Int32 - no
difference whatsoever. Why use int, then, apart from a bit less
typing? On 64-bit systems it will translate to 'System.Int64' - i.e.
use the appropriate size for the machine it's running on.

Wrong. C# int is System.Int32 - always. Read the language specifications,
please.
In C# (in IL generally) there is no such thing as a 'primitive type'.
Right.

Everything is a class or an object. There are two types of classes -

Wrong.

Besides classes, there are STRUCTS. Actually, everything is an INSTANCE of a
TYPE - either based on a CLASS or based on a STRUCT, or actually on an ENUM.

The word "object" is mostly reserved for instances of classes, but often
used for instances of structs, too. But technocally, everything is NOT a
class or an object - everything is a TYPE or an instance of a type.
Value types and Reference types. Value types are like primitive types
- more so than reference types anyway - but the importasnt thing to
remember is that there are NO 'primitive' types in the sense that that
is usually used.

Right so far. Though this is contradicting what you said further up.
int (that is, System.Int32) is not an object. It is a class. As a

No, it is NOT a class. System.Int32 is a TYPE, actually a type of a STRUCT.
STRUCTS are NOT classes.

The use of the word "Class" is totally wrong here.
class it can - and in fact does - have static methods which is what
.Parse(..) is.

No, not as a class. As a TYPE. TYPE is the word you should use here.
Once again, forget primitive types! They don't exist!

Right.

Just please get your use of words clear. You use "Class" and "object" in
different contradicting meanings all over your post - and seldon im the
technically correct one.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
R

Rob Teixeira [MVP]

Ok, "value type" if you want to get picky :)

Whether or not there are "primitive" types in the CLI (or more specifically
in C#) is a little debatable. For starters, an instance of Int32 produces a
single 32-bit value in memory. Additionally, in C# specs (4.1.3), all the
integral types, decimal types, and bool are defined as a special case of
Value Type referred to as "simple type". They have do in fact behave
slightly differently from normal STRUCTs. Specifically:
* instances can be created in code by use of literals
* when all operands of an expression are literal constants of Simple Type,
the compiler can evaluate the expression at compile time. "Expressions
defined by other struct types are not considered constant expressions."
* By using "const", you can declare constants of Simple Types. "It is not
possible to have constants of other struct types"
* "Conversions involving simple types can participate in evaluation of
conversion operators defined by other struct types, but a user-defined
conversion operator can never participate in evaluation of another
user-defined operator"

-Rob Teixeira [MVP]
 
S

Simon Smith

On Sun, 4 Jan 2004 18:37:29 +0100 in article
<[email protected]> in
microsoft.public.dotnet.languages.csharp , "Thomas Tomiczek [MVP]"

Wow! Can of worms time! OK -
Wrong. C# int is System.Int32 - always. Read the language specifications,
please.

Apologies and thanks to all who corrected me. I thought what I said
was true, but obviously not. Thanks to all for teaching me!
Wrong.

Besides classes, there are STRUCTS. Actually, everything is an INSTANCE of a
TYPE - either based on a CLASS or based on a STRUCT, or actually on an ENUM.

I think the next clause of my sentence shows that I know that there
are value and reference types. The word I should have used instead of
Class was Type.
The word "object" is mostly reserved for instances of classes, but often
used for instances of structs, too. But technocally, everything is NOT a
class or an object - everything is a TYPE or an instance of a type.

I know an object is an instance of a type.
Right so far. Though this is contradicting what you said further up.

Probably I'm being stupid, but I don't see how I contradicting myself.
No, it is NOT a class. System.Int32 is a TYPE, actually a type of a STRUCT.
STRUCTS are NOT classes.

The use of the word "Class" is totally wrong here.

OK, I'll take that. I didn't want to get into ADT's etc, I wanted to
emphasise that there was no such think as a primitive, just two types
of what I called a class for emphasis.
No, not as a class. As a TYPE. TYPE is the word you should use here.

OK already!
Right.

Just please get your use of words clear. You use "Class" and "object" in
different contradicting meanings all over your post - and seldon im the
technically correct one.
I don't think I do that. I only used the word 'object' once I think
Causing confusion about class and type I will accept, but not about
type (or class) and object. But whatever.

I learnt from your and Jon Skeet's and Jon Jagger's posts, so thanks!
 
S

Simon Smith

On Sun, 4 Jan 2004 16:57:52 -0000 in article
<[email protected]> in
microsoft.public.dotnet.languages.csharp , "Jon Jagger"
I would argue that they do and they don't. There are mappings to CIL but
when your programming in C# you are programming in C# and not in CIL. From
that point of view I would say that C# does have "primitive types" or more
accurately (as defined by the ECMA standard) it has simple types (11.1.3).
It all depends on your perspective.
HTH
Cheers
Jon Jagger

That's something I didn't know - I had assumed that they were standard
value types (albeit sealed). Thanks!
 
S

Simon Smith

On Sun, 4 Jan 2004 16:41:19 -0000 in article
<[email protected]> in
microsoft.public.dotnet.languages.csharp , Jon Skeet [C# MVP]
No it won't. In C#, int is *always* defined as a shorthand for Int32,
thank goodness.
Thanks for putting me right.

I had thought that the purpose of it was to help the transition
between 32 and 64 bit machines, mapping to an integer of the native
word size. (I have vague memories of going from VB 3 to VB4+ where
integer stayed 16 bits and needed to be changed to long IIRC for
performance. But I didn't think it through.....)
Thinking about it, that doesn't make sense because then what's the
purpose of decimal and string etc? Not that they need the same
purpose, but anyway.
I guess it must be a readability thing....

Cheers!
 
W

Willy Denoyette [MVP]

Simon Smith said:
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<[email protected]> in
microsoft.public.dotnet.languages.csharp , "Matt"

Once again, forget primitive types! They don't exist!

While they may be named simple types by higher level languages like C#, they
do exist at the CLR level.
Primitive types as defined in mscorlib have a special encoding in their
signatures, this is done for performance reasons.
There is even a CLR implemtated function called CorIsPrimitiveType thtat
returns true if a type is primitive.

Willy.



 
G

Gianluca Varenni

Willy Denoyette said:
While they may be named simple types by higher level languages like C#, they
do exist at the CLR level.
Primitive types as defined in mscorlib have a special encoding in their
signatures, this is done for performance reasons.
There is even a CLR implemtated function called CorIsPrimitiveType thtat
returns true if a type is primitive.

I think you are completely true: although "int" is just a sort of "shortcut"
for "System.Int32" in C#, there are special instruction in CIL to deal with
what we usually call "primitive types" (John Gough calls them "build-in
types" in his book "Compiling for the .NET CLR"). As a consequence, these
types are special; if not, the compiler should generate a method call to add
two numbers, something like Int32.Add(Int32 a, Int32 b).

Have a nice day
GV

 
J

Jon Skeet [C# MVP]

Willy Denoyette said:
There is even a CLR implemtated function called CorIsPrimitiveType thtat
returns true if a type is primitive.

Similarly there's Type.IsPrimitive:

<quote>
Property Value
true if the Type is one of the primitive types; otherwise, false.

Remarks
The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32,
UInt32, Int64, UInt64, Char, Double, and Single.
</quote>
 
W

Willy Denoyette [MVP]

Jon,

Absolutely right, and Type.IsPrimitive calls the internal function
CorIsPrimitiveType.

Thanks,

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top