C# OOP


Content


Structs, Classes
Encapsulation
Inheritance
Polymorphism
Other Lang Aspects
Example: Basic OOP app
Example: Dijkstra Algorithm





Structs, Classes

Structs, Classes

  • Encapsulate belonging set of data and behaviors
  • Members
    • Methods
    • Properties
    • Events, etc.
  • Class is blueprint for object instances created at run-time
  • namespace test {
        // class A (type A), with members Number and foo
        public class A {
             public int Number { get; set; }
    
             public int foo(int num) {
                 return (Number - 2) * num;
             }
        }
    }
    

Structs, Classes #2


  • Class defines a custom type
  • Create instances (objects) at run-time
  • Class is reference type!
  • Struct is value type!
namespace test {
    public class A { ... };

    static void Main() {
        // variable a has type A, is an instance (object) of class A
        A a = new A(); 
    }
}

Members


  • Methods, Fields, Constants
  • Properties, Events, Indexers
  • Constructors / Desctructors
  • Operators, Nested Types
  • These are the members of the type

Pillars of OOP


  1. Encapsulation 
  • Hide implementation details 
  • Protect data integrity
  • Inheritance
    • Promote code reuse
  • Polymorphism
    • Treat related objects in similar way




    Encapsulation

    Encapsulation


    • Specify accessibility of class members
    • public: type or member can be accessed
    • private: type or member only accessible in same class or struct
    • protected: Same as private but also accessible in derived classes
    • internal: Accessible in the same assembly
    • protected internal: ....

    Encapsulation #2

    • Classes, Structs  declared in namespace can be public or internal (default: internal)
    • Struct members (including nested classes and structs): public, internal or private
    • Class members (including nested classes, structs): public, protected internal, protected, internal or private
    • Default is private
    • Derived classes cannot have greater accessiblity than base type

    Inheritance

    • Classes support inheritance
    • No inheritance for structs
    • A class may derive (inherit) from another class
      • Base class
      • Derived class
    • Derived class inherits
      • Public, protected and internal members of base class
      • Except constructors, destructors  

    Inheritance #2


    • Reuse, extend and modify the behavior that is defined by base classes
    • Derived class can only have 1 direct base class
    • Derived class is a "specialization" of the base class
    • Cannot derive from sealed classes
    • Abstract classes
      • 1+ methods of class have no implementation
      • "Uncomplete" abstract class
      • Cannot create instances of abstract classes
      • But can serve as base class

    Inheritance #3

    • Virtual methods in base class may be overriden in derived class
    • Abstract methods in base class must be overriden in non-abstract derived class
    • Abstract and virtual members are the basis for polymorphism
    • Interface
      • Similar to the abstract class concept
      • But only consists of abstract members
      • Classes may "implement" interfaces
      • Classes may implement 1+ interfaces and derive from 1 direct base class 




    Polymorphism

    Polymorphism

    • Objects of a derived class may be treated as object of base class
      • Method parameters
      • In collections or arrays
    • Derived classes may override virtual members of base class
    • The run-time resolves and evaluates the derived implementation




    Other Language Aspects

    Properties


    • Provide read and write mechanism for private fields
    • class A {
          private int x = 1;
      
          public int X {
             get { return x * 2; }
             set { x = value / 2; } 
          }
      }
      
    • More compact way to write a setter and getter method

    Enumeration Types

    • Define a set of named integral constants
    • enum Days { Sunday, Monday, TuesDay, ... };
      
      Days today = Days.Friday;
    • Underlying data type is int
    • Specify values for enumeration
    • enum State {
         PowerOff = 0,
         Running = 2,
         Pause = 1,
         ....
      }
      

    Indexers

    • Allow instances of classes or structs to be indexed like arrays
    • Class MyArray {
         private int[] arr = new int[100];
      
         public int this[int i] {
             get { return arr[i] }
             set { arr[i] = value }
         }
      }
      Class Program {
         static void Main() {
             MyArray arr = new MyArray();
             arr[0] = 42;
             System.Console.WriteLine(arr[0]); 
         }
      }
      

    Nullable Types

    • Represent the specified type plus null
    • int? num = null;  // int range + null
      num = 123;        // or assign a usual value
      bool? foo = null; // true, false, null
      etc..
      
    • Value types that can be null
    • Reference types already are nullable
    • T? is shortform for Nullable<T>
    • ?? operator
    • int? x = null;
      int y = x ?? -1;
      System.Console.WriteLine(y); // ?



    References








    Thank you for your attention! 

    C# OOP

    By dinony

    C# OOP

    • 195