Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make a method out of this console write-line mess?

class WriteLine
{
    public static void Red(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Red;

        Console.WriteLine(input);
    }

    public static void Cyan(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Cyan;

        Console.WriteLine(input);
    }


    public static void Yellow (string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Yellow;

        Console.WriteLine(input);
    }


    public static void Purple(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Magenta;

        Console.WriteLine(input);
    }


    public static void White(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.White;

        Console.WriteLine(input);
    }


    public static void Black(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Black;

        Console.WriteLine(input);
    }

    public static void Green(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Green;

        Console.WriteLine(input);
    }

}
class Write
{
    public static void Red(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Red;

        Console.Write(input);
    }

    public static void Cyan(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Cyan;

        Console.Write(input);
    }


    public static void Yellow(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Yellow;

        Console.Write(input);
    }


    public static void Purple(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Magenta;

        Console.Write(input);
    }


    public static void White(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.White;

        Console.Write(input);
    }


    public static void Black(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Black;

        Console.Write(input);
    }

    public static void Green(string input)
    {
        Console.WriteLine();

        Console.ForegroundColor = ConsoleColor.Green;

        Console.Write(input);
    }
}

Is there a way to shorten this and make it more compact, like into a method, I was thinking something so that I could use WriteLine.Red("test"); and it would print red text. New to C# any tips would be greatly appreciated

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could use this class for all your use cases:

public class ConsoleLineWriter
{
    public void WriteLine(ConsoleColor color, string text)
    {
        Console.ForegroundColor = color;
        Console.WriteLine(text);
    }

    public void Write(ConsoleColor color, string text)
    {
        Console.ForegroundColor = color;
        Console.Write(text);
    }
}

Demo:

public static void Main()
{
    var writer = new ConsoleLineWriter();
    writer.WriteLine(ConsoleColor.DarkBlue, "Foo");
    writer.WriteLine(ConsoleColor.Green, "Bah");
    writer.Write(ConsoleColor.Red, "SameLine1");
    writer.Write(ConsoleColor.Cyan, "SameLine2");
    writer.Write(ConsoleColor.White, null); // back to default
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading