如果一个类有一个公共入口点,它将所需的一切都作为参数,它应该是静态的吗?

AJ.*_*AJ. 3 .net c# static

我刚刚编写了一个类,并意识到它的所有公共功能都封装在一个方法中.它没有属性,没有共享资源,并且不需要构造函数重载并处理它使用的任何内容.它看起来像这样:

public class ReportGenerator
{
    public string GenerateReport(List<SomeClass> stuffToReportOn)
    {
        string fileName = String.Empty;
        using(var reportDs = CreateDataSet(stuffToReportOn))
        {
            //do the stuff with the third party tool that 
            //creates the report.
            //Construct the filename.
            //Save the report.
        }
        return fileName;
    }

    private TypedDataSetRequiredByThirdPartyLib CreateDataSet(List<SomeClass> reportItems)
    {
        //create the dataset, calling two other private methods
        //to build the tables/rows
    }
}
Run Code Online (Sandbox Code Playgroud)

在我完成重构之后,我意识到这个类可能完全是静态的.我的问题是,应该吗?是否应将在一个公共方法中封装其所有功能的类设为静态?

smi*_*man 5

不,有什么预期的好处?

更重要的是潜在的错误.如果在你的类中声明一个静态变量,它只会被初始化一次,它的值将持续存在,并且每个调用的行为可能会有所不同.容易过度查看,这可能很难调试.