473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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!
Nov 15 '05 #1
14 7357
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]

"Matt" <ma*******@hotmail.com> wrote in message
news:eu**************@TK2MSFTNGP10.phx.gbl...
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!

Nov 15 '05 #2
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<eu**************@TK2MSFTNGP10.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Matt"
<ma*******@hotmail.com> wrote:
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!


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!

--
Simon
simon dot smith at snowvalley dot com
"Insomnia is a small price to pay for the stuff you read on UseNet"
Nov 15 '05 #3
Simon Smith <si****************@snowvalley.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

"Simon Smith" <si****************@snowvalley.com> wrote in message
news:vn********************************@4ax.com...
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<eu**************@TK2MSFTNGP10.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Matt"
<ma*******@hotmail.com> wrote:
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!


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


Nov 15 '05 #5

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:eo*************@tk2msftngp13.phx.gbl...
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)
Nov 15 '05 #6

"Simon Smith" <si****************@snowvalley.com> wrote in message
news:vn********************************@4ax.com...
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<eu**************@TK2MSFTNGP10.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Matt"
<ma*******@hotmail.com> wrote:
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!


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)
Nov 15 '05 #7
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]

"Thomas Tomiczek [MVP]" <t.********@thona-consulting.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:eo*************@tk2msftngp13.phx.gbl...
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)

Nov 15 '05 #8
On Sun, 4 Jan 2004 18:37:29 +0100 in article
<ua**************@TK2MSFTNGP12.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Thomas Tomiczek [MVP]"
<t.********@thona-consulting.com> wrote:

Wow! Can of worms time! OK -

"Simon Smith" <si****************@snowvalley.com> wrote in message
news:vn********************************@4ax.com.. .
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<eu**************@TK2MSFTNGP10.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Matt"
<ma*******@hotmail.com> wrote:
>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 thecase, >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.


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


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


Probably I'm being stupid, but I don't see how I contradicting myself.
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.


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


OK already!
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.

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!
--
Simon
simon dot smith at snowvalley dot com
"Insomnia is a small price to pay for the stuff you read on UseNet"
Nov 15 '05 #9
On Sun, 4 Jan 2004 16:57:52 -0000 in article
<ur**************@TK2MSFTNGP12.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Jon Jagger"
<jo*@jaggersoft.com> wrote:

"Simon Smith" <si****************@snowvalley.com> wrote in message

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


That's something I didn't know - I had assumed that they were standard
value types (albeit sealed). Thanks!
--
Simon
simon dot smith at snowvalley dot com
"Insomnia is a small price to pay for the stuff you read on UseNet"
Nov 15 '05 #10
On Sun, 4 Jan 2004 16:41:19 -0000 in article
<MP************************@msnews.microsoft.com > in
microsoft.public.dotnet.languages.csharp , Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Simon Smith <si****************@snowvalley.com> wrote:
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.

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!
--
Simon
simon dot smith at snowvalley dot com
"Insomnia is a small price to pay for the stuff you read on UseNet"
Nov 15 '05 #11

"Simon Smith" <si****************@snowvalley.com> wrote in message
news:vn********************************@4ax.com...
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<eu**************@TK2MSFTNGP10.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Matt"
<ma*******@hotmail.com> wrote:

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.

--
Simon
simon dot smith at snowvalley dot com
"Insomnia is a small price to pay for the stuff you read on UseNet"

Nov 15 '05 #12

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eF**************@TK2MSFTNGP11.phx.gbl...

"Simon Smith" <si****************@snowvalley.com> wrote in message
news:vn********************************@4ax.com...
On Sat, 3 Jan 2004 18:38:22 -0800 in article
<eu**************@TK2MSFTNGP10.phx.gbl> in
microsoft.public.dotnet.languages.csharp , "Matt"
<ma*******@hotmail.com> wrote:

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


Willy.

--
Simon
simon dot smith at snowvalley dot com
"Insomnia is a small price to pay for the stuff you read on UseNet"


Nov 15 '05 #13
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14
Jon,

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

Thanks,

Willy.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
3
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
1
by: Claire | last post by:
"Comparison to integral constant is useless; the constant is outside the range of type 'int'" How can I fix this compiler warning, please, for the following pseudocode? int m_nError; m_nError =...
2
by: mmiikkee13 | last post by:
>>a_list = range(37) .... print k, v .... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable What 'int' object is this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.