C# abstract struct

前端 未结 9 1916
闹比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条回答
  •  萌比男神i
    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...

提交回复
热议问题