0

We all know Struct is value type. If we see below struct it contains 2 field members one integer and another is string. We also know string is reference type. So, how following struct is value type?

    struct Student  
    { 
        public int Roll_no;  
        public string Name;  
    }                                                                             
    Student student = new Student();
    student.Roll_no = 1;
    student.Name = "Manas";
Manas Kumar
  • 2,411
  • 3
  • 16
  • 23
  • 1
    Perhaps this can help: https://stackoverflow.com/a/9334231/194717 – Tony Aug 13 '22 at 18:15
  • Why shouldn't it be? It's a pair of two *values* ‒ one integer and one object reference. – IS4 Aug 13 '22 at 18:19
  • Does this answer your question? [Why reference types inside structs behave like value types?](https://stackoverflow.com/questions/40564712/why-reference-types-inside-structs-behave-like-value-types) – Charlieface Aug 13 '22 at 23:41

1 Answers1

0

The object of this structure will have an integer on the stack and a string reference, which point to place in heap, where placed value of this string, but i need to mention, struct can stored in heap in some cases

Red Star
  • 556
  • 3
  • 13
  • _"The object of this structure will have an integer on the stack"_ - not quite: a value-type or `struct` member of a `class` (such as `Int32`) won't live on the stack (until it's an operand), for example. – Dai Aug 13 '22 at 18:17
  • @Dai, yep, forget to mention, thanks – Red Star Aug 13 '22 at 18:23