Re: Value Types and Reference Types
- From: "Jon Slaughter" <Jon_Slaughter@xxxxxxxxxxx>
- Date: Tue, 01 Apr 2008 17:54:32 GMT
"Marc Gravell" <marc.gravell@xxxxxxxxx> wrote in message
news:uCEZw5BlIHA.6092@xxxxxxxxxxxxxxxxxxxxxxx
So your saying that an int is not special? What I mean is that the
compiler has internal types that essentially are representable by
the cpu's logic.
Well, the compiler writes IL, and the CLR defines certain IL instructions
for some types - for instance the "add" instruction. This is a *little*
different to user-code, in that normally you would have an operator
(static op_Addition method) against the type [which isn't the case for
Int32].
It is the JIT's job to map the IL instructions to CPU instructions, and
there is no fundamental guarantee that this is a direct CPU operation. But
in reality it tends to be.
well, maybe not but a meta type at least. Its a "container" of value
types(although this isn't necessary I suppose).
"struct" has no real meaning except for as a C# type declaration, and as a
generic constraint. A value-type instance may contain either value-types
or references to reference-types.
Ok, well, there are objects of int type. I thought Int32 was the object
representative of int.
You can box an Int32==int, but you can box any type.
unless boxing doesn't create an object on the heap but just copiest the
value type? In that cause its still like an paired down object.
The box /is/ on the heap, but yes: it just copies the value.
Yes, but the main point was that boxing is slow. Creating objects from
classes is necessary but boxing isn't(maybe in certain cases of course
but there is a difference)
Compared to what? It isn't *very* slow. I wouldn't bend a design out of
shape just to avoid boxing; chances are that the indirection you'd need to
accomodate this would nillify any saving. Generics are a good way of
avoiding boxing, but have their own limitations.
I've read they were slow and from my own experience I've had some issues.
Slow is relative. I'm not saying avoid it but that you need to be aware. If
you are boxing a lot of stuff in a loop then it can be significantly slower.
Its just something to be aware of.
Sorry; I don't mean to sound negative, but the int vs Int32 probably
caused a lot of unnecessary confusion here. So to reiterate - they are
exactly the same thing.
Yes, I was wrong there. I thought the Int32 was the reference type for int.
I'm not sure why I thought that even when I used it before(or at least saw
it in other code). I suppose I thought because they explicity had two
different names then they should be different. I thought I read somewhere
that there were reference based equivalents for all primitive types but I
guess not? i.e., everything derives from system.object? (if so then there
are but I guess the primitive types do not have explicit corresponding
reference types?)
.
- Follow-Ups:
- Re: Value Types and Reference Types
- From: Peter Duniho
- Re: Value Types and Reference Types
- References:
- Re: Value Types and Reference Types
- From: Jon Slaughter
- Re: Value Types and Reference Types
- From: Jon Skeet [C# MVP]
- Re: Value Types and Reference Types
- From: Jon Slaughter
- Re: Value Types and Reference Types
- From: Marc Gravell
- Re: Value Types and Reference Types
- Prev by Date: Re: Value Types and Reference Types
- Next by Date: Re: Common Function?
- Previous by thread: Re: Value Types and Reference Types
- Next by thread: Re: Value Types and Reference Types
- Index(es):
Relevant Pages
|