I have been to an interview and was asked this question - is there any difference adding or removing the static keyword in these classes?

I know what static means but my understanding of this point is weak.

class Program
{
    static void Main(string[] args)
    {
            aAsposeNew.MultipleInstancesProblem();
            Console.ReadLine();
    }
}

public static class aAsposeNew
{
    public static void MultipleInstancesProblem()
    {
            var task1 = Task.Run(() => CreateDocument("document1.pdf"));
            var task2 = Task.Run(() => CreateDocument("document2.pdf"));
            var task3 = Task.Run(() => CreateDocument("document3.pdf"));
    }

    public static void CreateDocument(string documentName)
    {
        var doc = new Document();
        doc.Pages.Add();

        var table = new Aspose.Pdf.Table();
        var row = table.Rows.Add();
        table.ColumnWidths = "600";
        var hf =
            new HtmlFragment(@"<ul>
                                 <li>Internal HR Meeting Outcome</li>
                                 <li>Internal HR Meeting Outcome January 2015</li>
                               </ul>");
        var cell = row.Cells.Add();
        cell.Paragraphs.Add(hf);
        doc.Pages[1].Paragraphs.Add(table);

        doc.Save(@"C:\Output\" + documentName);
    }
  }

and without the static keyword:

class Program
{
    static void Main(string[] args)
    {
            aAsposeNew aAN = new aAsposeNew();
            aAN.MultipleInstancesProblem();
            Console.ReadLine();
    }
}

public class aAsposeNew
{
    public void MultipleInstancesProblem()
    {
            var task1 = Task.Run(() => CreateDocument("document1.pdf"));
            var task2 = Task.Run(() => CreateDocument("document2.pdf"));
            var task3 = Task.Run(() => CreateDocument("document3.pdf"));
    }

    public void CreateDocument(string documentName)
    {
        var doc = new Document();
        doc.Pages.Add();

        var table = new Aspose.Pdf.Table();
        var row = table.Rows.Add();
        table.ColumnWidths = "600";
        var hf =
            new HtmlFragment(@"<ul>
                                 <li>Internal HR Meeting Outcome</li>
                                 <li>Internal HR Meeting Outcome January 2015</li>
                               </ul>");
        var cell = row.Cells.Add();
        cell.Paragraphs.Add(hf);
        doc.Pages[1].Paragraphs.Add(table);

        doc.Save(@"C:\Output\" + documentName);
    }
  }
有帮助吗?

解决方案

No, there won't be any difference. The methods do not modify the state of the class, so having static or not having static has no effect. They are the exact same, minus the fact you need an instance of the class if it isn't static.

其他提示

In addition to the two options you posted (static method in static class, instance method in non-static class), there is a third option: a static method in a non-static class:

class Program
{
    static void Main(string[] args)
    {
        aAsposeNew.MultipleInstancesProblem();
        Console.ReadLine();
    }
}

public class aAsposeNew
{
    public static void MultipleInstancesProblem()
    {
        // elided
    }
}

I distinguish between the cases as if the class had some instance fields (or auto-properties), there would be a difference, as there'd be a memory impact from instantiating the class with new that you wouldn't get if it were a static method. For your code this doesn't matter as there are no instance fields or auto-properties.

This static method in non-static class case is still the same as the method being static in a static class, which, for your case, is the same as an instance method in an instance class (other than the way the method is called as @craysiii points out in his answer.

In this situation, as said, it will make no difference. In fact it would be a better practice to make the class static as the containing methods are static and there is no requirement to access any non-static instance members.

I use static class as a convenience to make it accessible for constants, exposing static methods to do independent units of work etc. A static class is loaded by the run time and lasts for the life of the app. Thus, we are guarenteed to always get the functionality we need without creating instances.

There wont be a difference in the functions execution but the manner in which it is called will be slightly different. I would suggest making it static, as it does not need a class instance to run.

许可以下: CC-BY-SA归因
scroll top