Is it possible to write extension methods for Console? [duplicate]

It’s not possible, as mentioned in Matt’s answer.

As a workaround you could create a static class that will wrap Console adding desired functionality.

public static class ConsoleEx
{
    public static void WriteLineRed(String message)
    {
        var oldColor = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(message);
        Console.ForegroundColor = oldColor;
    }
}

It’s not ideal, as you have to add that little “Ex”, but flows with code decently well, if that’s any (ehm) consolation:

ConsoleEx.WriteLineRed("[ERROR]")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)