Why do we need struct? (C#)

后端 未结 9 1911
盖世英雄少女心
盖世英雄少女心 2021-02-05 21:18

To use a struct, we need to instantiate the struct and use it just like a class. Then why don\'t we just create a class in the first place?

相关标签:
9条回答
  • 2021-02-05 21:23

    A major difference between the semantics of class and struct is that structs have value semantics. What is this means is that if you have two variables of the same type, they each have their own copy of the data. Thus if a variable of a given value type is set equal to another (of the same type), operations on one will not affect the other (that is, assignment of value types creates a copy). This is in sharp contrast to reference types.

    There are other differences:

    1. Value types are implicitly sealed (it is not possible to derive from a value type).
    2. Value types can not be null.
    3. Value types are given a default constructor that initialzes the value type to its default value.
    4. A variable of a value type is always a value of that type. Contrast this with classes where a variable of type A could refer to a instance of type B if B derives from A.

    Because of the difference in semantics, it is inappropriate to refer to structs as "lightweight classes."

    0 讨论(0)
  • 2021-02-05 21:24

    the difference is that a struct is a value-type I've found them useful in 2 situations

    1) Interop - you can specify the memory layout of a struct, so you can guarantee that when you invoke an unmanaged call.

    2) Performance - in some (very limited) cases, structs can be faster than classes, In general, this requires structs to be small (I've heard 16 bytes or less) , and not be changed often.

    0 讨论(0)
  • 2021-02-05 21:26

    Structs have many different semantics to classes. The differences are many but the primary reasons for their existence are:

    1. They can be explicitly layed out in memmory
      • this allows certain interop scenarios
    2. They may be allocated on the stack
      • Making some sorts of high performance code possible in a much simpler fashion
    0 讨论(0)
  • 2021-02-05 21:28

    A struct is a value type so if you create a copy, it will actually physically copy the data, whereas with a class it will only copy the reference to the data

    0 讨论(0)
  • 2021-02-05 21:33

    In C#, a struct is a value type, unlike classes which are reference types. This leads to a huge difference in how they are handled, or how they are expected to be used.

    You should probably read up on structs from a book. Structs in C# aren't close cousins of class like in C++ or Java.

    0 讨论(0)
  • 2021-02-05 21:34

    A struct is a value type (like Int32), whereas a class is a reference type. Structs get created on the stack rather than the heap. Also, when a struct is passed to a method, a copy of the struct is passed, but when a class instance is passed, a reference is passed.

    If you need to create your own datatype, say, then a struct is often a better choice than a class as you can use it just like the built-in value types in the .NET framework. There some good struct examples you can read here.

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