locked
Struct and Inheritance RRS feed

  • Question

  • User1904516115 posted

    Why Struct does not support inheritance?

    Thursday, December 27, 2018 11:04 AM

All replies

  • Thursday, December 27, 2018 12:15 PM
  • User1120430333 posted

    Why Struct does not support inheritance?

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/structs

    <copied>

    A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from ValueType, which inherits from Object.

    <end>

    Friday, December 28, 2018 5:24 AM
  • User-1174608757 posted

    Hi vinodkpasi 

      Essentially,Struct is a type of value, is not a type of reference.It is obvious that  type Int can't  inheritance a class.

     Another point is that struct has fixed size, Assuming that struct could  Inheritance   struct, So we could write as below: 

    struct Point {public int X; public int Y; } // 8 bytes
    struct Point3D : Point {public int X; } // 12 bytes
    Point[] points = new Point[5];
    //so we could write as points[0] = new Point3D();

     However ,It is obvious that we couldn't put the variable which is 12 bytes to another which is 8  bytes.

    Best regards 

    Wei Zhang.

    Monday, December 31, 2018 4:16 AM