Use of Interface to express constraint

let's first see some live example of how to use the Interfaces to express the idea of something abstract.

 

 

First of all, let 's see the Ikey interface and some of the other interfaces.

 

 

    public interface IKey<TEntity> : IKey, IQuery<TEntity>
    {
    }

    public interface IKey : IEquatable<IKey>
    {
    }

    public interface IQuery<TEntity> : IEquatable<IQuery<TEntity>>
    {
    }

 

 

So let's break down.

 

First of all, the IKey interface is inherit from IEquatable<IKey>, which implies that the IKey instance should be able to compare with another instance of IKey (which is the type parameter of base IEquatable).

 

then there is the Generic version of the interface, which is IKey<TEntity>; THis interface will inherit the IKey interface, which basically means that any Generic version of the Ikey interface is also a IKey and should be able to compare with any other IKey<TEntity> instance (the TEntity type parameter can can NOT different); --- or in other word, a Generic Form interface is also a type of the non-generic form of interfaces.

 

Last, let's see the IQuery interface, this interface is nearly the same as the IKey (non-generic form interface) but IQuery interface is a generic version of interface.

 

 

 

so by combining the interface together, it means that 

 

 

public interface IKey<TEntity> : IKey, IQuery<TEntity>

 

IKey<TEntity> is the an interface that can compare with IKey<AnotherTEntity> or can compare with Ikey or can compare with Key<TEntity> (the same type parameter).

 

Herei s one thought, why not to have the following signature?

 

 

public interface IKey<TEntity> : IKey, IEquatable<TEntity>
 

TODO:

 

add some concrete class that implements those inerfaces.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值