Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • 22
    There are some parts of this answer that are not quite right. Classes do not always go on the heap, and structs do not always go on the stack. Current exceptions include struct fields on a class, captured variables in anonymous methods and lambda expressions, iterator blocks, and the already mentioned boxed values. But stack vs heap allocation is a implementation detail and may be subject to change. Eric lippart discusses this here. I've downvoted, but will happily remove it if you update. Commented Oct 29, 2010 at 9:08
  • 2
    struct do not support inheritance from other stucts/classes, but you CAN implement an interface on a struct. Commented Feb 27, 2013 at 5:03
  • 2
    You might want to clarify what you mean when you claim that structs "Do not have a memory overhead per new instance". My first interpretation was that you were claiming - obviously absurdly - that structs use zero memory. Then I thought that maybe you're trying to say that a struct, unlike a class, requires exactly as much memory as the sum of its member fields, and no more. But then I Googled for c# struct memory overhead and found this answer by Hans Passant that says that no, that's not the case either. So what do you mean? Commented May 5, 2017 at 23:51
  • 5
    @MarkAmery I had the same initial reaction as you id to the expression "no memory overhead", but I think that the OP is referring to the fact that instances of class are managed memory (handled by the garbage collector), whereas instances of struct are not. Commented Nov 6, 2017 at 22:40
  • 2
    "Struct Are passed by value (like integers)" is false: all variable are passed by value, also the reference type. If you want to pass a variable by reference you have to use the "ref" keyword. jonskeet.uk/csharp/parameters.html#ref Commented Nov 8, 2017 at 5:58