C# abstract struct

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

    You can create interface IVertex and then add to Your structs.

    0 讨论(0)
  • 2021-01-11 15:17

    You can't, basically. You can't derive from a struct. Why do you think you want a struct instead of a class? You say "it has to be a value type" - why? Likewise do you think inheritance is the only option instead of (say) composition? For example, you could use:

    public struct Vertex
    {
        // ...
    }
    
    public struct Color
    {
        // ...
    }
    
    public struct ColorVertex
    {
        private readonly Color color;
        private readonly Vertex vertex;
    
        // ...
    }
    

    You're simply not going to be able to get an "abstract struct" or anything similar to work, so I suggest you explain the reasons behind your unsatisfiable requirements, instead of just stating them as requirements which can't be avoided.

    0 讨论(0)
  • 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()
       {
           ...
       }
    }
    
    0 讨论(0)
  • 2021-01-11 15:26

    Classes can be "value types" as well, (in the sense used in Domain Driven Design). All you have to do is make it immutable, make the constructors inaccessible publicly (Protected or internal), and create static factory methods to create instances of them and control their instantiation, and do not have any setters on your properties...

    NOTE: The phrase Value Type in this contect has nothing to do with Value type vs Reference Type. It has to do with Value Type vs Entity Type as used in Domain Drtiven Design or Domain Modeling...

    0 讨论(0)
  • 2021-01-11 15:27

    In C#, you can use interfaces to achieve something akin to polymorphism with value types (structs) as you can't derive directly from a struct but you can have multiple struct types implement specific interfaces.

    Therefore, instead of your abstract struct, Vertex, you can have an interface, IVertex.

    interface IVertex
    {
        int SizeInBytes { get; }
        void SetPointers();
    }
    

    However, it is exceedingly rare that you need to implement your own value types, so make sure you really need value type semantics before proceeding. If you do implement your own value types, make sure they're immutable as mutable value types are a gateway to all kinds of horrible problems.

    You should be aware that boxing will occur when casting from a value type to an interface. Not only does this have implications if your value types are mutable (don't make mutable value types), but this will decrease, or most likely cancel out any memory advantage you may gain from using a value type, depending on when or how you do this and whether you do it for every value - use a profiler if you're unsure.

    0 讨论(0)
  • 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)

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