Home > other >  Call a void method in Console.WriteLine
Call a void method in Console.WriteLine

Time:02-23

I am working on a C# console project, but I am yet totally new in C#. My question is: can I call a void method in Console.WriteLine();? Maybe something like

static void ChangeColor()
{
   Console.backgroundColor = ConsoleColor.Red
};

Console.WriteLine("Hello *Calling method *ChangeColor();* * my friend!"); 

An example from my code:

                Console.Write(
$@"
____________________________________________________________________________________________________
|                                                                                                  |
|                         __     __                                                                |
|                        / / \__/\ \                                                               |
|                        \/| /\/\|\/                                                               |");
                DarkBlueTxt();
                Console.Write(
$@"
|                        __||o o||__                                                               |
|                       / . \__/ .  \                                                              |
|                      /.  .(__).  . \                                                             |
|______________________\ .  /__\ .   /_____________________________________________________________|");

CodePudding user response:

Perhaps make yourself a helper method:

void WriteWithColors(params object[] p){

  foreach(var o in p){
    if(o is ConsoleColor)
      Console.BackgroundColor = (ConsoleColor)o;
    else
      Console.Write(o);
  }
}

And then call it like:

WriteWithColors(ConsoleColor.Red, @"
____________________________________________________________________________________________________
|                                                                                                  |
|                         __     __                                                                |
|                        / / \__/\ \                                                               |
|                        \/| /\/\|\/                                                               |", ConsoleColor.Blue, @"
|                        __||o o||__                                                               |
|                       / . \__/ .  \                                                              |
|                      /.  .(__).  . \                                                             |
|______________________\ .  /__\ .   /_____________________________________________________________|");

The compiler will bundle your mix of Colors and strings up into an object array and then the loop in the method goes through it either changing the color or printing the string depending on what it is (a color or a string)

You don't need $ on the head of a string if you don't use {placeholders} within the string btw

CodePudding user response:

You could check ANSI colors, but it requires a certain console mode which is explained in the link.

static void Main(string[] args)
{
    Console.WriteLine("\u001b[31mHello World!\u001b[0m");
}

Source: Using ANSI colour codes in .NET Core Console applications

CodePudding user response:

Just for fun, you can heavily abuse the new C# 10 Interpolated string handlers to let you write e.g.:

WriteColoredText($"First{ConsoleColor.Red}Second");

Please don't actually do this in practice -- it's a horrible hack which noone will understand. Use @CauisJard's answer instead: it's probably clearer, and much simpler. But this is a fun exploration of the power of the new interpolated string handlers.

With that said, here goes.

First, we'll need to define our own interpolated string handler, We'll wrap StringBuilder.AppendInterpolatedStringHandler, as that mainly does what we want. However, when a ConsoleColor is written, we'll record this, ending up with a list of segments, where each segment is a section of text and the color to write it in:

[InterpolatedStringHandler]
public struct ConsoleColorInterpolatedStringHander
{
    private readonly StringBuilder sb = new();
    private StringBuilder.AppendInterpolatedStringHandler appendHandler;
    private ConsoleColor? currentColor = null;
    public List<(ConsoleColor?, string)> Segments { get; } = new();
    
    public ConsoleColorInterpolatedStringHander(int literalLength, int formattedCount)
    {
        appendHandler = new(literalLength, formattedCount, sb);
    }

    public void AppendLiteral(string value) => appendHandler.AppendLiteral(value);
    public void AppendFormatted<T>(T value) => appendHandler.AppendFormatted(value);
    public void AppendFormatted<T>(T value, string? format) => appendHandler.AppendFormatted(value, format);
    public void AppendFormatted<T>(T value, int alignment) => appendHandler.AppendFormatted(value, alignment);
    public void AppendFormatted<T>(T value, int alignment, string? format) => appendHandler.AppendFormatted(value, alignment, format);
    
    public void AppendFormatted(ConsoleColor color)
    {
        Segments.Add((currentColor, sb.ToString()));
        sb.Clear();
        currentColor = color;
    }
    
    public void Finish()
    {
         Segments.Add((currentColor, sb.ToString()));   
    }
}

We then need to define a method which accepts this handler:

public static void WriteColoredText(ConsoleColorInterpolatedStringHanderhandler)
{
    handler.Finish();
    foreach (var (color, text) in handler.Segments)
    {
        if (color != null)
        {
            Console.BackgroundColor = color.Value;
        }
        Console.Write(text);
    }
}

We can then pass an interpolated string to our new WriteColoredText method. Any placeholders which contain a ConsoleColor cause the background color to change.

See it on SharpLab.

To get an idea of what's going on, switch to the C# view on SharpLab. The important bit is that the compiler has written our call to `WriteColoredText into:

public static void Main()
{
    ConsoleColorInterpolatedStringHander handler = new ConsoleColorInterpolatedStringHander(10, 1);
    handler.AppendLiteral("Hello");
    handler.AppendFormatted(ConsoleColor.Red);
    handler.AppendLiteral("World");
    WriteColoredText(handler);
}
  • Related