locked
Difference between Static method & Static class with static method? RRS feed

  • Question

  • User1954082887 posted

    I am going to make manager for my controllers. But stuck with architectural appoach.

    what is difference between Static method & Static class with static method?

    which one to be used?

    Saturday, January 19, 2013 12:40 AM

Answers

  • User354071898 posted

    The main features of a static class are:

    They only contain static members.

    They cannot be instantiated.

    They are sealed.

    They cannot contain Instance Constructors

    The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state.

    Static Method

    A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created.

    The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.

    Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

    Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances. Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class. Although a field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 19, 2013 10:52 AM

All replies

  • User-1419158234 posted

    It depends on your needs, kindly provide some details about your logic so we can find the most efficient way.

    very detailed description with example could be found here http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

    Saturday, January 19, 2013 10:32 AM
  • User-1247037355 posted

    Hai

    Check

    http://forums.asp.net/t/1355455.aspx/1

    Saturday, January 19, 2013 10:44 AM
  • User354071898 posted

    The main features of a static class are:

    They only contain static members.

    They cannot be instantiated.

    They are sealed.

    They cannot contain Instance Constructors

    The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state.

    Static Method

    A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created.

    The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.

    Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

    Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances. Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class. Although a field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 19, 2013 10:52 AM