Why do we need struct? (C#)

后端 未结 9 1912
盖世英雄少女心
盖世英雄少女心 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:36

    One of the main reasons is that, when used as local variables during a method call, structs are allocated on the stack.

    Stack allocation is cheap, but the big difference is that de-allocation is also very cheap. In this situation, the garbage collector doesn't have to track structs -- they're removed when returning from the method that allocated them when the stack frame is popped.

    • edit - clarified my post re: Jon Skeet's comment.
    0 讨论(0)
  • 2021-02-05 21:37

    All of the reasons I see in other answers are interesting and can be useful, but if you want to read about why they are required (at least by the VM) and why it was a mistake for the JVM to not support them (user-defined value types), read Demystifying Magic: High-level Low-level Programming. As it stands, C# shines in talking about the potential to bring safe, managed code to systems programming. This is also one of the reasons I think the CLI is a superior platform [than the JVM] for mobile computing. A few other reasons are listed in the linked paper.

    It's important to note that you'll very rarely, if ever, see an observable performance improvement from using a struct. The garbage collector is extremely fast, and in many cases will actually outperform the structs. When you add in the nuances of them, they're certainly not a first-choice tool. However, when you do need them and have profiler results or system-level constructs to prove it, they get the job done.

    Edit: If you wanted an answer of why we need them as opposed to what they do, ^^^

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

    This is a myth that struct are always created on heap. Ok it is right that struct is value type and class is reference type. But remember that

    1. A Reference Type always goes on the Heap.
    2. Value Types go where they were declared.

    Now what that second line means is I will explain with below example

    Consider the following method

      public void DoCalulation()
      {
            int num;
            num=2; 
      }
    

    Here num is a local variable so it will be created on stack.

    Now consider the below example

    public class TestClass
    {          
        public int num;
    }
    public void DoCalulation()
    {
        TestClass myTestClass = new TestClass ();
        myTestClass.num=2;
    }
    

    This time num is the num is created on heap.Ya in some cases value types perform more than reference types as they don't require garbage collection.

    Also remeber:

    The value of a value type is always a value of that type. The value of a reference type is always a reference.

    And you have to think over the issue that if you expect that there will lot be instantiation then that means more heap space yow will deal with ,and more is the work of garbage collector.For that case you can choose structs.

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