an I prevent a specific type using generic restrictions

后端 未结 4 607
悲&欢浪女
悲&欢浪女 2021-01-18 13:03

I have an overload method - the first implementation always returns a single object, the second implementation always returns an enumeration.

I\'d like to make the m

4条回答
  •  囚心锁ツ
    2021-01-18 14:07

    constraints don't support exclusion, which may seem frustrating at first, but is consistent and makes sense (consider, for example, that interfaces don't dictate what implementations can't do).

    That being said, you could play around with the constraints of your IEnumerable overload...maybe change your method to have two generic typings with a constraint like "where X : IEnumerable" ?

    ETA the following code sample:

      void T[] GetOrAdd (string cachekey, Func fnGetItem) 
                where X : IEnumerable
        { 
        }
    

提交回复
热议问题