Is it possible to create an instance of a class on the stack?
I know that in C++ you can create an instance of a class on the stack like MyClass mc = MyClass(8.2); or on the heap like MyClass * mc = new MyClass(8.2); Can you do the same thing in C#? The only way I ever create a class in C# is by new ing it. No, it's not possible. All instances of all classes are always allocated on the heap. It is value types, including user defined struct types, that hold values, rather than references to values elsewhere, that can store a value in whatever location that variable happens to store its value in, which may not be the heap. 来源: https://stackoverflow.com