When should I use a struct rather than a class in C#?

前端 未结 28 2636
予麋鹿
予麋鹿 2020-11-21 11:55

When should you use struct and not class in C#? My conceptual model is that structs are used in times when the item is merely a collection of value types. A way to

相关标签:
28条回答
  • 2020-11-21 12:02

    Here is a basic rule.

    • If all member fields are value types create a struct.

    • If any one member field is a reference type, create a class. This is because the reference type field will need the heap allocation anyway.

    Exmaples

    public struct MyPoint 
    {
        public int X; // Value Type
        public int Y; // Value Type
    }
    
    public class MyPointWithName 
    {
        public int X; // Value Type
        public int Y; // Value Type
        public string Name; // Reference Type
    }
    
    0 讨论(0)
  • 2020-11-21 12:02

    First: Interop scenarios or when you need to specify the memory layout

    Second: When the data is almost the same size as a reference pointer anyway.

    0 讨论(0)
  • 2020-11-21 12:02

    I use structs for packing or unpacking any sort of binary communication format. That includes reading or writing to disk, DirectX vertex lists, network protocols, or dealing with encrypted/compressed data.

    The three guidelines you list haven't been useful for me in this context. When I need to write out four hundred bytes of stuff in a Particular Order, I'm gonna define a four-hundred-byte struct, and I'm gonna fill it with whatever unrelated values it's supposed to have, and I'm going to set it up whatever way makes the most sense at the time. (Okay, four hundred bytes would be pretty strange-- but back when I was writing Excel files for a living, I was dealing with structs of up to about forty bytes all over, because that's how big some of the BIFF records ARE.)

    0 讨论(0)
  • 2020-11-21 12:03

    You need to use a "struct" in situations where you want to explicitly specify memory layout using the StructLayoutAttribute - typically for PInvoke.

    Edit: Comment points out that you can use class or struct with StructLayoutAttribute and that is certainly true. In practice, you would typically use a struct - it is allocated on the stack vs the heap which makes sense if you are just passing an argument to an unmanaged method call.

    0 讨论(0)
  • 2020-11-21 12:03

    Following are the rules defined at Microsoft website:

    ✔️ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

    ❌ AVOID defining a struct unless the type has all of the following characteristics:

    It logically represents a single value, similar to primitive types (int, double, etc.).

    It has an instance size under 16 bytes.

    It is immutable.

    It will not have to be boxed frequently.

    for further reading

    0 讨论(0)
  • 2020-11-21 12:06

    Use a struct when you want value semantics as opposed to reference semantics.

    Edit

    Not sure why folks are downvoting this but this is a valid point, and was made before the op clarified his question, and it is the most fundamental basic reason for a struct.

    If you need reference semantics you need a class not a struct.

    0 讨论(0)
提交回复
热议问题