How to restrict T to value types using a constraint?

后端 未结 6 1809
情歌与酒
情歌与酒 2020-12-16 11:46

I want to restrict the possible types N can take-on using a constraint. I wish to restrict N to be either a int or a decimal.

public static Chart PopulateIn         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-16 12:08

    As Pieter said, you cannot use a compile-time check to to this. However, you can do the following at runtime:

    if(!(typeof(N).equals(typeof(int32))) && !(typeof(N).equals(typeof(decimal))))
      // do something
    

提交回复
热议问题