C# FAQS : Class Modifiers , Static constructors , Destructors , Indexers

C# FAQS : Class Modifiers , Static constructors , Destructors , Indexers

Class

Various class modifiers are listed below :

  1. new

  2. public

  3. protected

  4. internal

  5. private

  6. abstract

  7. sealed

  8. static

Class – new modifier

The new modifier is permitted on nested classes. It specifies that the class hides an inherited member by the same name. It is a compile-time error for the new modifier to appear on a class declaration that is not a nested class declaration.

Class – Abstract modifier

An abstract class cannot be sealed.

Class – sealed modifier

A sealed class cannot also be an abstract class.

Class – static modifier

A static class declaration is subject to the following restrictions:
·         A static class may not include a sealed or abstract modifier. Note, however, that since a static class cannot be instantiated or derived from, it behaves as if it was both sealed and abstract.
·         A static class may not include a class-base specification  and cannot explicitly specify a base class or a list of implemented interfaces. A static class implicitly inherits from type object.
·         A static class can only contain static member. Note that constants and nested types are classified as static members.
·         A static class cannot have members with protected or protected internal declared accessibility.

Static constructors

Static constructors have the following properties:
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.

Destructors

  • Destructors cannot be defined in structs. They are only used with classes.
  • A class can only have one destructor.
  • Destructors cannot be inherited or overloaded.
  • Destructors cannot be called. They are invoked automatically.
  • A destructor does not take modifiers or have parameters.
  • Indexers Overview


    • Indexers enable objects to be indexed in a similar way to arrays.
    • A get accessor returns a value. A set accessor assigns a value.
    • The this keyword is used to define the indexers.
    • The value keyword is used to define the value being assigned by the set indexer.
    • Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism.
    • Indexers can be overloaded.
    • Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array.

    Leave a Reply