using extension methods on int

后端 未结 7 1194
难免孤独
难免孤独 2021-02-19 04:10

I\'m reading about extension methods, and monkeying around with them to see how they work, and I tried this:

namespace clunk {
    public static class oog {
             


        
7条回答
  •  青春惊慌失措
    2021-02-19 04:32

    Unfortunately you cannot introduce new operators, or implement support for existing operators, on types, through extension methods.

    Basically, what you want to do cannot be done.

    The only way to introduce new operators is to put them in one of the involved types. For binary operators you can put them in your own type, provided your type is one of the two, for unary types you need to put the operator inside the type itself.

    Since you can't extend an int in any way, you can't do it.

提交回复
热议问题