3

In C# types are said to be of value and reference types

enter image description here

If System.Object is the topmost class so does it mean everything is object?

If its true ... then these questions come with them

1. so all types must be reference types as we cannot assign value directly to object .....

2.

    int a=20; //this is primitive type or object ???
    int a = new int(); //according to documentation this is reference type ... 

How these two statements are different ... In first statement does the new keyword get automatically called?

Please explain this diagram its confusing me a lot ... couldn't understand from msdn.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
swapyonubuntu
  • 1,952
  • 3
  • 25
  • 34
  • It is worth knowing about [the stack and heap](http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx) if you want to learn more about value/reference types. – jbabey Jun 09 '13 at 16:35
  • 5
    @jbabey - No, no, no. Where an item is stored is an implementation detail. – H H Jun 09 '13 at 16:36
  • This both a duplicate and not the right kind of question for SO. – H H Jun 09 '13 at 16:37
  • 2
    @jbabey unless you're working for Microsoft, is that really important? In daily programming all you really have to worry is their interactions and functionality, not how they work behind the scenes. It's sadly a very common interview question about where they're stored, but all it does is tell the interviewer that the developer knows some nuance that likely won't help them on the job. – Yuriy Faktorovich Jun 09 '13 at 16:38
  • 1
    @YuriyFaktorovich - "...is that really important?" - it's important to know enough to understand the performance implications of boxing value types. – Joe Jun 09 '13 at 16:45
  • The meta-answer is that the compiler can do whatever it wants to. So it stores the contents of the value type on the stack instead of just a pointer, copies said contents as appropriate, (un)boxes them when necessary, etc. The diagram illustrates the *logical* relationship between the types, not how the compiler / runtime handles them. – millimoose Jun 09 '13 at 16:49
  • @Joe like millimoose said, you don't necessarily know whether boxing/unboxing will have performance implications. Right now the Microsoft implementation might. But is that really important either? Unless you're developing an application where a millisecond matters, I doubt it. – Yuriy Faktorovich Jun 09 '13 at 16:59
  • _according to documentation this is reference type_ : faulty docs. – H H Jun 09 '13 at 17:25
  • @HenkHolterman More likely it's badly read docs, but it's impossible to tell without a link. My intuitive understanding of that line is that `new int()` is an expression that creates a boxed integer, thus a reference-typed one, but the assignment to an `int` would unbox it again. – millimoose Jun 09 '13 at 17:55

1 Answers1

1

Value types exist in two "flavors": As pure value types and as boxed values types. A boxed value type is an immutable reference object. Even if these two have a different "physical" nature, their logical C# type is the same. I.e. a boxed int is of type int. This is different from Java where there exists a logical value integer type and a logical reference integer type that are to related but distinct types.

To make this work, C# has an automatic boxing mechanism.

object  o = 5; // Automatically boxes the integer number 5;

Unboxing must be specified explicitly:

int i = (int)o;
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188