我希望能够向控制台打印出三种类型的消息:警告、错误和成功。为此,每次我都必须将控制台的 ForegroundColor 更改为黄色、红色或绿色,打印出消息并将颜色改回。为了加快速度,我决定创建一个类(比方说 Printer),它具有三种方法:Warning(message)、Error(message),成功(消息)。现在我的问题是:Printer 应该是 struct 还是 class?我不打算在这个类中有更多的字段/方法。
请您参考如下方法:
实际上听起来这应该是一个静态类。
public static class Printer
{
public static void Warning(string message)
{
var currentColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(message);
Console.ForegroundColor = currentColor;
}
//Other similar methods here
}
然后你会这样调用它
Printer.Warning("This is a warning");
