classes in c# programming

Class in C# Language-: A class is a data structure that may contain data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors, and static constructors), and nested types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class.

Class Declarations -:  A class declaration that supplies a type-parameter-list is a generic class declaration. Additionally, any class nested inside a generic class declaration or a generic struct declaration is itself a generic class declaration, since type parameters for the containing type must be supplied to create a constructed type.

A class declaration cannot supply type-parameter-constraints-clauses unless it also supplies a type-parameter-list.

Class Modifiers-:  A class-declaration may optionally include a sequence of class modifier

  • new
  • public
  • protected
  • internal
  • private
  • abstract
  • sealed
  • static

Abstract Classes-: The abstract modifier is used to indicate that a class is incomplete and that it is intended to be used only as a base class. these follow ..

  • An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on an abstract class. While it is possible to have variables and values whose compile-time types are abstract, such variables and values will necessarily either be null or contain references to instances of non-abstract classes derived from the abstract types.
  •  An abstract class is permitted (but not required) to contain abstract members.
  •  An abstract class cannot be sealed.

For a example

abstract class A
{
public abstract void F();
}

abstract class B: A
{
public void G() {}
}

class C: B
{
public override void F() {
// Actual implementation of F
}
}

Sealed Classes-:The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class.

Static Classes-:The static modifier is used to mark the class being declared as a static class. A static class cannot be instantiated, cannot be used as a type, and can contain only static members. Only a static class can contain declarations of extension methods

partial Modifier-: The partial modifier is used to indicate that this class-declaration is a partial type declaration. Multiple partial type declarations with the same name within an enclosing namespace or type declaration combine to form one type declaration.

Type Parameters-: A type parameter is a simple identifier that denotes a placeholder for a type argument supplied to create a constructed type. A type parameter is a formal placeholder for a type that will be supplied later. By contrast, a type argument is the actual type that is substituted for the type parameter when a constructed type is created.

type-parameter-list:
< type-parameters>

type-parameters:
attributesopt type-parameter
type-parameters , attributesopt type-parameter

type-parameter:
identifier

Class Base Specification-:A class declaration may include a class-base specification, which defines the direct base class of the class and the interfaces  directly implemented by the class.
class-base:
: class-type
: interface-type-list
: class-type , interface-type-list

interface-type-list:
interface-type
interface-type-list , interface-type
The base class specified in a class declaration can be a constructed class type A base class cannot be a type parameter on its own, although it can involve the type parameters that are in scope

Base Classes-: When a class-type is included in the class-base, it specifies the direct base class of the class being declared. If a class declaration has no class-base, or if the class-base lists only interface types, the direct base class is assumed to be object. A class inherits members from its direct base class, as described In the example
class A {}
class B: A {}
class A is said to be the direct base class of B, and B is said to be derived from A. Since A does not explicitly specify a direct base class, its direct base class is implicitly object.

Interface Implementations-: A class-base specification may include a list of interface types, in which case the class is said to directly implement the given interface types.

Leave a Reply

Your email address will not be published. Required fields are marked *