2

If i have a class Person and this class have data members as FirstName, LastName.

 public Class Person
 {
     public string firstName {get; set;}
     public string lastName   {get; set;}

 }

Class Person is a Reference Type but firstName and lastName are Value Type. Then how this would be stored? Would Class Person be stored on Heap and firstName and lastName would be stored on stack? Can someone please help me understand how this works exactly?

Cactus
  • 27,075
  • 9
  • 69
  • 149
user3478233
  • 87
  • 2
  • 8
  • its all on the heap. Google would be a better resource than SO for this kind of question. – Crowcoder Feb 27 '15 at 01:37
  • 2
    [Here](http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx)'s some good reading on the subject of value types. The most important point, I feel, is C# works hard to make it irrelevant - you don't have to care about stack vs heap. – Blorgbeard Feb 27 '15 at 01:38
  • 7
    Also, `string` is a [reference type](http://stackoverflow.com/questions/1069155/is-string-a-value-type-or-a-reference-type). – Blorgbeard Feb 27 '15 at 01:39
  • by the way string is immutable reference-types. It is very important the word !!!immutable!!! – Bassam Alugili Feb 27 '15 at 10:50

1 Answers1

3

In this case, everything will be stored on the heap, because everything is contained within a reference type.

Note also that string is a reference type anyway (although MS does everything to make it look like a value type).

Thomas Weller
  • 11,631
  • 3
  • 26
  • 34