C# Object-Oriented Programming OOP


Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects".

paradigm : a typical example or pattern of something; a model

Objects may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

Object is instance of class, which typically also determines the type of object.

Encapsulation

Encapsulation is defined 'the process of enclosing one or more items within a physical or logical package'.
Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.
Encapsulation in OOP prevents access to implementation details.
Encapsulation is implemented by using access specifiers.
An access specifier defines the scope and visibility of a class member.

C# Access Specifiers

  • public : The type or member can be accessed by any other code in the same assembly or another assembly that references it.
  • private : The type or member can only be accessed by code in the same class.
  • protected : The type or member can only be accessed by code in the same class or in a derived class.
  • internal : The type or member can be accessed by any code in the same assembly, but not from another assembly.
  • protected internal : The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.
  • private protected : The type or member can be accessed by code in the same class or in a derived class within the base class assembly.


Inheritance

Inheritance describes the ability to create new classes based on an existing class.
The class whose members are inherited is called the base class.
The class that inherits those members of the base class is called the derived class.
All classes in C# implicitly inherit from the Object class.
A C# developer can specify only one base class for a derived class.
By default all classes can be inherited in C#.
Using keyword sealed to specify that a class cannot be used as a base class.

seal : fasten or close securely

Using keyword abstract to specify that a class can be used as a base class only and cannot be instantiated.

Overriding Members

By default, a derived class inherits all members from its base class.
Overriding members can change the behavior of the inherited members.
Overriding members is to define a new implementation of the method, property or event in the derived class.

C# Modifier

  • virtual : Allows a class member to be overridden in a derived class.
  • override : Overrides a virtual (overridable) member defined in the base class.
  • abstract : Requires that a class member to be overridden in the derived class.


Interface

Interfaces define a set of properties, methods, and events.
Interfaces do not provide implementation.
A class that implements an interface must implement every aspect of that interface.

Generics

Classes, structures, interfaces and methods in the .NET Framework can include type parameters that define types of objects that they can store or use.
The most common example of generics is a collection, where the user can specify the type of objects to be stored in a collection.

Polymorphism

Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.

Events

Events enable a class or object to notify other classes or objects when something of interest occurs.

Static Member

A static member of the class is a property, procedure, or field that is shared by all instances of a class.
Static members cannot access non-static properties, fields or methods.

Static Class

Static classes in C# have static members only and cannot be instantiated.

Anonymous Types

Anonymous types enable you to create objects without writing a class definition for the data type.
anonymous : (of a person) not identified by name; of unknown name.

Object-oriented programming

Object-Oriented Programming (C#)

C# - Encapsulation

張貼留言

0 留言