Is it possible define an extension operator method?

前端 未结 10 821
迷失自我
迷失自我 2021-01-04 02:09

is it possible to define an extension method that at the same time is an operator? I want for a fixed class add the possibility to use a known operator that actually can\'t

10条回答
  •  轮回少年
    2021-01-04 02:28

    No, you can't have an extension method which is also an operator. Extension methods can only be declared in static classes, which can't have instances and according to the C# spec,

    User-defined operator declarations always require at least one of the parameters to be of the class or struct type that contains the operator declaration. [7.3.2]

    Therefore, it is impossible for an extension method to also be an overloaded operator.

    Additionally, you can't override System.String since it is a sealed class.

提交回复
热议问题