0

So I have this:

class SimpleDateStructureDemo
{

  struct Date
  {
    public int year;
    public int month;
    public int day;
  }

  static void Main()
  {
    ...
  }

}

And then I see an example, saying that the object is allocated on the heap:

Date dateMoonWalk = new Date();

I thought that classes are ref type, and structs are value type. In the end, you CAN create a struct type object on the heap by just using new, right?

trincot
  • 317,000
  • 35
  • 244
  • 286
user997578
  • 35
  • 1
  • 1
  • 7
  • 2
    Have you ever considered indenting your code? Also, when you are referencing something you've seen or read, it generally makes sense to cite/link to it, so it can be evaluated along with your question. – Jonathon Reinhart Jun 23 '14 at 23:17
  • 2
    When you say "heap" what do you mean? (I know what it means, I want to know what you think it means) And why do you care if your object is allocated on the heap, on the state, or inregesered in to the CPU. – Scott Chamberlain Jun 23 '14 at 23:18
  • 1
    Required Reading: http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx – Michael Edenfield Jun 23 '14 at 23:20
  • http://stackoverflow.com/questions/4853213/c-sharp-struct-stack-allocated-or-sometimes-heap-allocated?rq=1 , http://stackoverflow.com/questions/203695/does-using-new-on-a-struct-allocate-it-on-the-heap-or-stack?rq=1 , http://stackoverflow.com/questions/5563774/dilemma-with-using-value-types-with-new-operator-in-c-sharp?lq=1 – user2864740 Jun 23 '14 at 23:21
  • 1
    The fact that Jon Skeet's answer to this question (6 years ago) is like 3 pages long should clue you in that this is way more complex than you think it is, and almost certainly not relevant to the everyday developer anymore – Michael Edenfield Jun 23 '14 at 23:25
  • You indeed can `object dateMoonWalk = new Date();` which is probably the closest way to your "create a struct type object on the heap by just using `new`", but I think it already covered in great details in duplicate FAQ-like question. – Alexei Levenkov Jun 23 '14 at 23:28

2 Answers2

3

you CAN create a struct type object on the heap by just using new, right?

No, if you do that inside of Main, in general, it won't get allocated on the heap. You can allocate a struct on the heap in many ways, though, including using it as a field in a class, using it in a closure, etc.

The new Date syntax just initializes the struct to it's default (zeroed out) value, but doesn't actually change how or where it's allocated.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I don't know if "you can allocate a struct on the heap" is an exact description. Maybe the struct is allocated on the stack (or in a register) and then boxed into the heap. – Thomas Weller May 29 '21 at 22:13
2

new in this case has nothing to do with allocation on the heap

The new operator is also used to invoke the default constructor for value types.

http://msdn.microsoft.com/en-us/library/fa0ab757.aspx

adjan
  • 13,371
  • 2
  • 31
  • 48