MTo
MTo

Reputation: 406

c# how to assign Console.WriteLine to variable

I Would like to shorten typing Console.WriteLine and assign it to var for example: var write = Console.WriteLine(); and execute it like write("My name is.."); >>>output>> My name is...

Upvotes: 4

Views: 9475

Answers (9)

This is completely and utterly useless, but if you want to do it anyways, you can define a delegate like this:

delegate void ProcessString(string input);.

Now, create a ProcessString variable and pass it the Console.WriteLine callable:

ProcessString write = new ProcessString(Console.WriteLine);.

Then you can just call write and it will behave completely like Console.WriteLine.

Upvotes: 0

MrClan
MrClan

Reputation: 6780

Adding to @dasblinkenlight answer above. Modifying the delegate signature to

delegate void WriteDelegate(string msg = "", params object[] args);

will now let you now call with 0 parameters as well.

private static readonly WriteDelegate w = Console.Write;
w("Hello");
w(", {0}", "world!");
w(); // Calling with no parameter

Upvotes: 0

Habib
Habib

Reputation: 223267

You can have a static method Write like:

public static void Write(string value)
{
    Console.WriteLine("My name is.." + value);
}

and then you can call it like:

Write("SomeName");

With C# 6.0 you have an option to use static import: You can do:

using static System.Console;

and then in your method you can do:

WriteLine("My name is...");

EDIT: Since the release of Visual Studio 2015 CTP, in January 2015, static import feature requires explicit mention of static keyword like:

using static System.Console;

Upvotes: 4

Andrew
Andrew

Reputation: 7880

A more flexible option is to use a delegate. It allows you to write both a simple string or a formatted one.

class Program
{
    delegate void writeDelegate(string format, params object[] arg);

    static void Main(string[] args)
    {
        writeDelegate write = Console.WriteLine;

        write("Simple text.");
        write("Formatted {0}: {1}", "text", 10);
        Console.ReadKey();
    }
}

Anyway, as others pointed out, this is a bit awkward; I wouldn't do this. You are adding the risk of confusing the developer who reads your code, who may end up being your future self.

Upvotes: 2

wblanks
wblanks

Reputation: 598

What you're looking for is an Action. Use it like this:

Action<string> write = Console.WriteLine;
write("Your message here");

This will do what you need.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

Although doing it simply to shorten your code is a questionable idea, here is what you can do: define a delegate for the Write method, like this

delegate void WriteDelegate(string msg, params object[] args);

Define a variable of type WriteDelegate, and assign Console.Write to it:

private static readonly WriteDelegate w = Console.Write;

Now you can use w to call Console.Write, with or without parameters:

w("Hello");
w(", {0}", "world!");

Demo.

Upvotes: 5

Servy
Servy

Reputation: 203826

Assigning a method to a variable is done though the use of a delegate:

Action<string> write = Console.WriteLine;
write("Hello World!");

Of course the delegate won't be able to represent every single overload of Console.WriteLine (and it has 18 of them). It can only represent one of the overloads (but it can represent any one of those 18).

Upvotes: 10

Matt Klinker
Matt Klinker

Reputation: 773

You could always define your own method and defer to the Console static instance. However, what I think you're really after won't be ready until c# 6.0 and is a feature named "Static Using Statmenets", you can read more about it here: http://msdn.microsoft.com/en-us/magazine/dn683793.aspx

Upvotes: 1

user1017882
user1017882

Reputation:

void write(string message)
{
    Console.WriteLine(message);
}

Usage:

write("log this");

NOTE: I'd re-think the case and naming convention of your method. It should really be Write.

Upvotes: 0

Related Questions