Re: Value Types and Reference Types




"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?)


.



Relevant Pages

  • Re: Value Types and Reference Types
    ... Well, the compiler writes IL, and the CLR defines certain IL instructions ... shape just to avoid boxing; chances are that the indirection you'd need to ... I don't mean to sound negative, but the int vs Int32 probably caused ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ArrayList vs. List<>
    ... boxing means referring to a block of memory by pointer and it is only used ... array of complex numbers is represented as a float array internally in the ... was to say that strings aren't boxed because they're already reference ... mean that strings are handled as an inline char array but the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract class variables question
    ... I think I understand boxing a little better now. ... the object that is on the heap. ... value types are copied to the heap and made into an object and reference ... String types are already reference types and all we are doing when we do ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What is the purpose of %fs?
    ... instructions in the tls/libc that reference %fs. ... Where could I find information about the contents of that segment? ... It does crash at a different location where it reads from an already freed location. ...
    (comp.os.linux.development.system)
  • Free 360 not fake
    ... I've put step by step instructions at the bottom of the post... ... who joins uses the reference nearest the top of the list, ... - follow the instructions for registering on this site - it involves ... recommend not ticking the boxes to sign up to their email lists! ...
    (uk.games.video.xbox)