简体   繁体   中英

It is possible to write only WriteLine instead of Console.WriteLine in C#?

I'm a completely noob and a begginer programmer in C# but I was reading about Roslyn and 'What's new in C# 7.0' and I found something very interesting that I can't find out the answer I need.

In this link , all the examples given contain something like WriteLine("something"); instead of Console.WriteLine("something"); , for example:

public void PrintCoordinates(Point p)
{
  p.GetCoordinates(out int x, out int y);
  WriteLine($"({x}, {y})");
}

My question is: How can I do that?

Would something like this work?

public static void WriteLine(string v) => Console.WriteLine(v);

Try using static directive:

 using static System.Console;

 ...

 WriteLine("some text");

Starting with C# 6.0, this is possible:

using static System.Console;

However, previous versions of C# do not have static imports.

您可以使用一个Action

Action<string> WriteLine = (text) => Console.WriteLine(text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM