Static Keyword in C# .Net

Static Class:
  1. A static class cannot be instantiated
  2. Only contains static members (property/methods/fields)
  3. Static class cannot implement an interface
  4. Static class cannot have any instance members (property/methods)
  5. Static class can’t declare any constructors and the compiler doesn’t create a parameter-less constructor by default
  6. Static class are implicitly abstract, you can’t add the abstract modifier yourself
  7. Static class may be generic
  8. Static class may be nested, in either non-static or static class
  9. Static class may have nested types, either non-static or static
  10. Only static, top-level non-generic class can contain extension methods (3.0)
  11. A static class is never instantiated
  12. static keyword on a class enforces that a type not be created with a constructor
  13. A static constructor initializes static fields before accessing

Static Method:
  1. Cannot access non-static class level members (property/method)
  2. Static methods can be public or private
  3. Static methods are by default private
  4. Static properties are similar to static methods
  5. Static methods have no way of accessing fields that are instance fields however instance methods can access static fields
  6. Static method can access non-static fields only by passing instance fields as parameters using its non-static class’s object in which static method is available
  7. Static method can have only non-static parameters
  8. Any type of method(static/non-static) cannot have static members

No comments:

Post a Comment

Static Keyword in .Net

Static Class: Ø    A static class  cannot  be  instantiated Ø    Only  contains   static members  (property/methods/fields) ...