C# Console.WriteLine Method

Soldato
Joined
2 May 2004
Posts
19,950
Hi,

I've got the following method:
Code:
        public void OutputSuccess(string Message)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(Message);
            Console.ResetColor()
        }

Is there any way I can use it like this?

Code:
OutputSuccess("Successfully downloaded {0}", File)

Obviously the above gives an error as it thinks I'm trying to pass File to the method as well.

I'm new to C# so forgive me if this is stupidly easy :D

Thanks.
 
Soldato
OP
Joined
2 May 2004
Posts
19,950
Figured out you can use String.Format like this:

Code:
OutputSuccess(String.Format(Successfully downloaded {0}", File))

Is there any way to avoid this? It'd be nice just to use OutputSuccess() rather than OutputSuccess(String.Format())

Thanks
 
Last edited:
Soldato
OP
Joined
2 May 2004
Posts
19,950
Sorted

Code:
        public void OutputSuccess(string Message, params object[] Args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(String.Format(Message, Args));
            Console.ResetColor();
        }
 
Associate
Joined
7 Nov 2013
Posts
255
Location
Kent, England
Sorted

Code:
        public void OutputSuccess(string Message, params object[] Args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(String.Format(Message, Args));
            Console.ResetColor();
        }

The String.Format is not needed here, as you can see here there is an overload of Console.WriteLine which performs the equivalent of String.Format so you can change your code to

Code:
        public void OutputSuccess(string Message, params object[] Args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(Message, Args);
            Console.ResetColor();
        }

But nonetheless, great job on finding a solution! :D
 
Back
Top Bottom