OOP
Object oriented programming Polymorphism Encapsulation Inheritance OOP
Class concepts Classes can contain: Constants Delegates Events Fields Constructors Destructors Properties Methods Nested classes
Encapsulation 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 be accessed only by code in the same class or struct. Protected The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class. Internal The type or member can be accessed by any code in the same assembly, but not from another assembly.
Access modifiers
Access modifiers. Combined.
Access modifiers. Protected internal.
Access modifiers. Protected internal.
Access modifiers. Protected internal.
Access modifiers. Auto-property.
Access modifiers. Best practice Reasons to prefer public property over public field Serialization Reflection Testing Debugging Logging Used for data binding
Useful keywords Readonly Const declaration or in a constructor initialized at the declaration Value is set when instance is created Value is set once before compiling
Inheritance & Polymorphism No multiple inheritance in C# Classes can be inherited Interfaces can be implemented
Interfaces Can do logics Interfaces can contain: Properties Events Methods Indexers Always public Cannot be initiated directly Classes can implement one or more interfaces
Abstract classes Is a logics Can contain whatever a usual class can Cannot be initialized directly Class can inherit only one abstract class
Abstract classes vs interfaces Feature Interface Abstract class Multiple inheritance Default implementation Access Modfiers A class may inherit several interfaces. An interface cannot provide any code, just the signature. An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public A class may inherit only one abstract class. An abstract class can provide complete, default code and/or just the details that have to be overridden. An abstract class can contain access modifiers for the subs, functions, properties
Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Homogeneity Speed Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface If various implementations only share method signatures then it is better to use Interfaces. Requires more time to find the actual method in the corresponding classes An abstract class defines the core identity of a class and there it is used for objects of the same type. If various implementations are of the same kind and use common behaviour or status then abstract class is better to use. Fast
Abstract classes vs interfaces Feature Interface Abstract class Adding functionality (Versioning) Fields and Constants If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. No fields can be defined in interfaces If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. An abstract class can have fields and constrants defined
Overriding
Overriding
Inheritance
Composition
Composition over inheritance Prefer composition over inheritance do not use a compose-always approach it's easy to change behavior on the fly with Dependency Injection / Setters with composition Inheritance is more rigid
Composition over inheritance Inheritance acid rule: Does TypeB want to expose the complete interface (all public methods no less) of TypeA such that TypeB can be used where TypeA is expected? Prefer composition Does TypeB only want only some/part of the behavior exposed by TypeA?
Multiple interfaces implementation
Implicit implementation
Explicit implementation
Explicit vs implicit implementation Implicit always public Explicit always private No virtual for explicit No abstract for explicit
Design patterns In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.
Singleton
Lazy initialization
Abstract factory
Other interesting patterns These patterns you will definitely need: - Strategy - Decorator - Prototype - Builder - Facade - Dependency injection - Mediator
Generics Powerful tool since c# 2.0 Point classes: Should contain values with floating points Should contain integer values Should log values every time the coordinates are changed
Generics
Generics
Generics with where
business, evolved.