Can I add extension methods to an existing static class?

前端 未结 15 1747
慢半拍i
慢半拍i 2020-11-22 07:23

I\'m a fan of extension methods in C#, but haven\'t had any success adding an extension method to a static class, such as Console.

For example, if I want to add an e

15条回答
  •  心在旅途
    2020-11-22 07:27

    You can't add static methods to a type. You can only add (pseudo-)instance methods to an instance of a type.

    The point of the this modifier is to tell the C# compiler to pass the instance on the left-side of the . as the first parameter of the static/extension method.

    In the case of adding static methods to a type, there is no instance to pass for the first parameter.

提交回复
热议问题