Why Do I need to redeclare type constraint in generic subclass

后端 未结 3 1994
故里飘歌
故里飘歌 2021-02-07 10:06

Recently I tried to create a generic subclass by implementing a generic interface.

public interface IModule where T : DataBean { ..... }
public class Mo         


        
3条回答
  •  走了就别回头了
    2021-02-07 10:35

    The constranints on the interface are too vauge to tell the compiler what constraints the Module class should have. For example, the Module class could have a constraint on a super-class (inherited class) of DataBean.

    I don't know the wisdom of the C# designers. Because the constraints can be different, I think a decision was made to have the developer explicitly declare the constraint rather than having the compiler make assumptions.

提交回复
热议问题