0

It's said that class objects are allocated on heap using new, and struct object can declare directly on stack. But if I use "new" to allocate a struct, where is it stored? Like this:

struct MyStruct
{
    public int mI;
    public string mS;
}

MyStruct s1;
s1.mI = 1;
s1.mS = "ab";

MyStruct s2 = new MyStruct
{
    mI = 2,
    mS = "xy"
};

I know "s1" is on heap, what about "s2"?

Thanks a lot.

trincot
  • 317,000
  • 35
  • 244
  • 286
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • 1
    *stack/heap* is an *implementation detail*, is there an reason this concerns you? – TheGeneral Jun 11 '19 at 09:22
  • 1
    Have you read this https://blogs.msdn.microsoft.com/ericlippert/2010/09/30/the-truth-about-value-types/ – Rahul Jun 11 '19 at 09:26
  • 1
    @Thangadurai not necessarily always – Rahul Jun 11 '19 at 09:28
  • 1
    Usually there´s no reason to think about where an object is stored, this could even be on a disc or any arbitrary storage and should not affect the **semnatics** of your object - which is **value-types** and **reference-types**. Those are the terms you should care for. – MakePeaceGreatAgain Jun 11 '19 at 09:31

0 Answers0