Can I add extension methods to existing static classes?

I love extension methods in C, but I didn't succeed in adding them to static classes such as Console.

For example, if I want to add an extension called "WriteBlueLine" to the console so that I can do the following:

Console.WriteBlueLine("This text is blue");

I tried this by adding a local, public static method (using Console as the "this" parameter) But no dice!

public static class Helpers {
    public static void WriteBlueLine(this Console c, string text)
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine(text);
        Console.ResetColor();
    }
}

This doesn't add the 'WriteBlueLine' method to the console... Did I do it wrong? Or is it impossible to ask?

#1 building

Perhaps you can add a static class with a custom namespace and the same class name:

using CLRConsole = System.Console;

namespace ExtensionMethodsDemo
{
    public static class Console
    {
        public static void WriteLine(string value)
        {
            CLRConsole.WriteLine(value);
        }

        public static void WriteBlueLine(string value)
        {
            System.ConsoleColor currentColor = CLRConsole.ForegroundColor;

            CLRConsole.ForegroundColor = System.ConsoleColor.Blue;
            CLRConsole.WriteLine(value);

            CLRConsole.ForegroundColor = currentColor;
        }

        public static System.ConsoleKeyInfo ReadKey(bool intercept)
        {
            return CLRConsole.ReadKey(intercept);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteBlueLine("This text is blue");   
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
        }
    }
}

#2 building

You cannot add a static method to a type. You can only add (pseudo) instance methods to instances of types.

The point of this modifier is to tell the C compiler to pass the instance to the left. As the first parameter of the static / extended method.

In the case of adding a static method to a type, the first parameter has no instance to pass.

#3 building

Yes, in a limited sense.

public class DataSet : System.Data.DataSet
{
    public static void SpecialMethod() { }
}

This works, but Console cannot because it is static.

public static class Console
{       
    public static void WriteLine(String x)
    { System.Console.WriteLine(x); }

    public static void WriteBlueLine(String x)
    {
        System.Console.ForegroundColor = ConsoleColor.Blue;
        System.Console.Write(.x);           
    }
}

This is possible, as long as it is not in the same namespace. The problem is that you have to write a proxy static method for each method of System.Console. This is not necessarily a bad thing, because you can add the following:

    public static void WriteLine(String x)
    { System.Console.WriteLine(x.Replace("Fck","****")); }

Or

 public static void WriteLine(String x)
    {
        System.Console.ForegroundColor = ConsoleColor.Blue;
        System.Console.WriteLine(x); 
    }

It works by attaching something to the standard WriteLine. It could be a number of lines or a wrong word filter, and so on. Whenever WebProject1 is said and the namespace System is imported when Console is specified in the namespace, WebProject1.Console is selected on System.Console as the default value for these classes in namespace WebProject1. Therefore, as long as you never specify System.Console.WriteLine, this code changes all Console.WriteLine calls to blue.

#4 building

When I learned the extension method without success, I tried to use System.Environment for this operation. As others have said, the reason is that extending a method requires an instance of a class.

#5 building

No The extension method definition requires an instance of the type you want to extend. Its misfortune; I'm not sure why it's needed

Posted on Tue, 14 Jan 2020 00:14:16 -0500 by happypete