我应该使用类或结构来包装方法吗?

时间:2016-09-12 14:38:34

标签: c#

我希望能够向控制台打印出三种类型的消息:警告,错误和成功。为此,每次我必须将控制台的ForegroundColor更改为黄色,红色或绿色时,请打印出消息并更改颜色。为了加快速度,我决定创建一个类(让我们说Printer),它有三种方法:Warning(message)Error(message)Success(message) 。现在我的问题是:Printer应该是struct还是class?我不打算在这个课程中有更多的领域/方法。

1 个答案:

答案 0 :(得分:4)

实际上听起来这应该是一个静态类。

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");
相关问题