C# abstract struct

前端 未结 9 1852
闹比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:28

    It looks like what you want is an interface.

    public interface IVertex
    {
        int SizeInBytes { get; }
        void SetPointers();
    }
    
    public struct ColorVertex : IVertex
    {
       private Vector3 Position;
       private Vector4 Color;
    
       public int SizeInBytes
       {
          get { return (3 + 4) * 4; }
       }
    
       public void SetVertexPointers() // Did you mean SetPointers?
       {
       }
    }
    

    An interface makes sense since all of your methods are declared abstract (this would mean it relies on deriving classes to implement the method which is essentially what an interface is)

提交回复
热议问题