Test if object implements interface

前端 未结 12 1590
情歌与酒
情歌与酒 2020-11-28 01:03

What is the simplest way of testing if an object implements a given interface in C#? (Answer to this question in Java)

12条回答
  •  盖世英雄少女心
    2020-11-28 01:18

    In addition to testing using the "is" operator, you can decorate your methods to make sure that variables passed to it implement a particular interface, like so:

    public static void BubbleSort(ref IList unsorted_list) where T : IComparable
    {
         //Some bubbly sorting
    }
    

    I'm not sure which version of .Net this was implemented in so it may not work in your version.

提交回复
热议问题