Difference between interface as type constraint and interface as parameter?

前端 未结 5 1355
暖寄归人
暖寄归人 2021-02-13 14:30

If I wanted to create a method that takes an instance of IList as a parameter (or any other interface, but let\'s use IList as an example), I could cre

5条回答
  •  囚心锁ツ
    2021-02-13 14:58

    Simple :

    here you gave a pretty easy example : since you already gave the top interface ILIST.

    but ,

    lets say i have an instanc Of Objects.

    JimMorrison
    JohnLennon
    SnowieWhite
    

    they are all from ILegends.

    they are all singers objects ( var singer = new Singer())

    YokOno is also a singer but not Ilegends ( mmmm ... wonder why?)

    and your function can take Singer.

    but you want to make sure that only ILegeneds will be valid... so here is your answer.

提交回复
热议问题