简体   繁体   中英

What is the difference between a static class and a normal class?

When should I prefer either a static or a normal class? Or: what is the difference between them?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace staticmethodlar
{
    class Program
    {
        static void Main(string[] args)
        {
            SinifA.method1();
        }
    }

    static class SinifA 
    {
       public static void method1()
        {
            Console.WriteLine("Deneme1");
        }
    }

    public static class SinifB
    {
        public static void method2()
        {
            Console.WriteLine("Deneme2");
        }
    }
    public class sinifC
    {
       public void method3()
        {
            Console.WriteLine("Deneme3");
        }
    }

    public class sinifD : sinifC
    {
        void method4()
        {
            Console.WriteLine("Deneme4");
        }

        sinifC sinifc = new sinifC();  // I need to use it :)
    }
}

Static classes contain static objects that can't be instantiated multiple times. Usually what I use static classes for are to house static methods that provide calculations, general processing patterns, string output formats, etc. Static classes are light weight and don't need instantiation.

For instance System.IO.File is a static class with static a method Exists() . You don't create a File object to call it. You invoke it like this

System.IO.File.Exists(filePath)

Rather than doing this

System.IO.File myFile = new System.IO.File(filePath);

if(myFile.Exists()) { /* do work */ }

If you require several objects in software, then you use dynamic classes. For instance if you have an inventory system you may have several Product objects and in that case you would use a dynamic class such as this

public class Product
{

    public int    ProductID   { get; private set; }
    public string ProductName { get; private set; }
    public int    Qty         { get; set; }

    public Product( int productID, string productName, int total )
    {
        this.ProductID = productID;
        this.ProductName = productName;
        this.Qty = total;
    }       
}
  • static classes cannot be instantiated or inherited .
  • static classes are marked as sealed and abstract by compiler in the output MSIL.
  • all members of static classes must be static as well.
  • only static classes can host extension methods .
  • static classes cannot be used as generic type arguments.

You can create instances of "normal" classes via the class constructor.

var normal = new Normal();

You cannot create instances of static classes. They can only have static methods.

Also worth noting is that you must declare extension methods in static classes.

From the MSDN documentation on static classes :

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

In simple terms:

A static class cannot be instantiated using the new keyword. All methods in a static class must be static and can be called directly without instantiation.

A "normal" class MUST be instantiated however any static methods in a "normal" class can be called without instantiation.

Non static methods in a "normal" class cannot be called without first instantiating the containing class.

Static members can be called without using an instance of the class. For example, a static Math class with an Area method call be called by Math.Area without instantiating the Math class first.

A static class is one that cannot be instantiated. Therefore, it cannot be used as a 'template" for multiple instances of some object that conforms to the class definition as a normal class can. In a normal class, the data members defined are created for each "instance" of the class, and each "instance" has it's own set of these data members, so these instances represent individual "entities" or complex in-memory data structures for some object (which you coded the class to represent).

When you define (code) a static class, all of it's data fields must also be static, meaning that they cannot be instantiated once for each instance of the class, (you can't create instances of a static class ). Static classes are useful only when you just need a container to hold methods that do not depend on the state of some object or data structure being managed by the application (like an add function, or a string formatter, etc.)

A normal class is one which you can instantiate and can use with objects.

A static class is one which can not be instansiated and can't be extended.

That means a static class is sealed and abstract by default, you may look at the

MSIL of a static class compiler puts sealed and abstract in front of a static class.

So, because a static class can't be instantiated, you can't have instance methods defined

on static classes, so all methods must be static and public ofcourse as you would want to

use them.

A static class is singleton by default.

Static classes are used for defining extension methods because you don't want to

instansiate them.

Static classes are like global provider of unique services.

I hope that make sense to you and will help you to understand the static classes.

It might help to think of "static" and "instance" in terms of memory addressing.

An object that is static exists in one and only one location in memory; you can't create instances of it because by definition it has been stated that it will be found in one particular location. It is "static" and unchanging. The creation of the object is implicit rather than explicit.

var result = Math.Pow(3, 3); // static - Math class can't be instanced

An object that isn't static can be instanced one or more times; you're saying, "Create for me an object of this type" and the object is given a memory address. You can create multiple instances that all exist in different memory addresses. The creation of the object is explicit using the new keyword.

var Ted = new Employee(); // instance - creates a new object at a new address

This is where people get into trouble in ASP .NET web programming; static classes are created once per Application Domain and their constructors are therefore only called once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM