Forget everything you know from C/C++. C# looks similar, but it is so different there's no point in carrying baggage over. Your question doesn't make sense.
Stack and heap are just an implementation detail on .NET - it might very well be allocated in a register for all you know.
Unlike C/C++, you can't choose where you allocate something. There's a few hints you have, but really, those are just implementation details. For example, no matter what you do, there's no working .NET runtime that will allow you to allocate a class
on stack. The only thing preventing someone from making that runtime is deciding how to do that safely - the specification doesn't prohibit it.
Do not rely on implementation details like this, unless you really have to. Instead, think about the way you want to use the types. Do you want value semantics? Use value types. Reference semantics? Use reference types.
new
simply calls the constructor of a given type. For struct
s, there's always a default constructor that simply zeroes out everything, and it's impossible to write your own constructor with no arguments (until C# 6, that is. Sadly.).
The code you have, of course, doesn't compile. You're using an unassigned variable. The same thing would work with a field, though. EDIT: As noted in the comments, it does compile, as long as you assign all the fields of the struct
. In a way, you're creating your own struct
constructor by doing that. Maybe it's a performance micro-optimization. I wouldn't use that syntax unless necessary - explicitly using new MyStruct()
or default(MyStruct)
is cleaner. Unless you can prove it makes a difference, I would simply use default
or a constructor.
Right now, you can use new MyStruct()
and default(MyStruct)
interchangeably - both simply zero out the relevant memory (whether that's something that actually happens is an implementation detail, but you will have a value with all zeroes). In fact, that's how .NET works for all types - every piece of memory you allocate will always be zeroes as a default.
There's places where C# is very similar to C/C++. But memory allocation certainly isn't one of those places :)