C# abstract struct

前端 未结 9 1904
闹比i
闹比i 2021-01-11 14:34

How can I achieve inheritance (or similar) with structs in C#? I know that an abstract struct isn\'t possible, but I need to achieve something similar.

I need it as

9条回答
  •  自闭症患者
    2021-01-11 15:21

    You can use interfaces

    interface IVertex 
    {
        int SizeInBytes();
        void SetPointers();
    }
    
    struct ColorVertex : IVertex
    {
       Vector3 Position;
       Vector4 Color;
    
       int SizeInBytes
       {
          get { return (3 + 4) * 4; }
       }
       void SetVertexPointers()
       {
           ...
       }
    }
    

提交回复
热议问题