Is static going to make any difference in this code snippet

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
}
}
</code>
<code>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); } } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
}
}
</code>
<code>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); } } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Program
{
static void Main(string[] args)
{
aAsposeNew.MultipleInstancesProblem();
Console.ReadLine();
}
}
public class aAsposeNew
{
public static void MultipleInstancesProblem()
{
// elided
}
}
</code>
<code>class Program { static void Main(string[] args) { aAsposeNew.MultipleInstancesProblem(); Console.ReadLine(); } } public class aAsposeNew { public static void MultipleInstancesProblem() { // elided } } </code>
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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật