Вопрос

I would like to add a custom WriteColoredText method to the Console Class, to acces like this:

Console.WriteColoredText(Parametters)

The problem is that I only found information about creating method extensions for specific datatypes but any about a method extension for a Class.

If this is not possibly, which similar alternatives I have?

Это было полезно?

Решение

The short answer is, you can't.

Extension methods can only apply to object instances, but the Console class is static. The only solution is to write your own, completely separate class, say ConsoleEx, and implement any methods you want there.

Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top