struct - what is it for?

前端 未结 10 2331
小鲜肉
小鲜肉 2020-12-17 23:42

I know something about struct type. But I can\'t understand: what is it for? when have I use it? Classes, simple value-types and enums - that\'s all that I need

相关标签:
10条回答
  • 2020-12-18 00:17

    Youcan use structs when you want a "class" with value (rather than reference) semantics.

    0 讨论(0)
  • 2020-12-18 00:18

    Simple value types are best implemented via a struct.

    Struct Usage Guidelines

    It is recommended that you use a struct for types that meet any of the following criteria:

    * Act like primitive types.
    * Have an instance size under 16 bytes.
    * Are immutable.
    * Value semantics are desirable.
    

    You must also understand that a class instance is allocated on the heap. A struct -is a vallue type- and is allocated on the stack.

    0 讨论(0)
  • 2020-12-18 00:24

    structs are for objects that represent something whose identity is defined by the values stored in it's properties rather than by an Id or key. These are called "Value types" as opposed tyo objects called "Entity Types", whose identity persists over time and is not dependant on the values of the properties of the object.

    As an example, a Person class (an Entity) has an identity that persists from one session to another, even from year to year, in spite of how the Person's address, phone number, employer, etc might change from one instance to another. If you inadvertently have two instances of a Person class in memory at the same time, which represent the same individual/entity, then it is important that they have the same values for their properties.

    A CalendarMonth object otoh, (a value type) only has identity defined by the value which specifies which calendar month it is... no matter how many instances of "March 2009" you might be using, they are all interchangeable and equivilent. Another example might be an object representing a FiscalYear designation in a tax program. A great example is an address object. (Not talking here about the asociation of an address with a person or a business, or any other entity, but just the address itself). Changing just about any property of an address makes it a different address. Once you create an address object in memory, it is equivilent and interchangeable with every other address object that has the same properties. This is why these value types should generally be immutable. When you need one, create a new one with the property value(s) you need, and throw it away when you're done using it.

    0 讨论(0)
  • 2020-12-18 00:25

    First you must understand the difference between value-type and reference type. I will assume since you said to skip that part that you understand what it is.

    Struct is a value-type and you get all of the privileges that you would get working with a value-type.

    • Structs are passed around by value. When you do something like

      DateTime time = new DateTime(); DateTime newTime = time; // you are not referencing time // instead you have created a new instance

    • Structs are NOT lightweight classes they may have many methods, just look at DateTime struct.

    • Structs maybe lighter in performance, but not all the time. Consider passing a large struct to a method. Because structs are value-types each time you pass one into a method a new instance of the struct is created, hence copying the struct each time. If you have a fairly large struct this will be a much larger performance hit.

    • You may have to occasionally box and unbox structs, since they are value types.

    In short, use a struct to represent an atomic value in memory.

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