Show Menu
Cheatography

Advanced C# - OOP, Inheritance, Interfaces, Events Cheat Sheet (DRAFT) by

A cheat sheet to get an overview and small code examples for advanced C# concepts that could help your object oriented programming and encapsulation

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Classes

Classes are building blocks of software which encaps­ulate data and behavior.

Anat­omy: Name, Fields, Methods

Decl­ara­tion: Acces­sMo­difier + class + ClassName

Cons­tru­cto­rs: (code snippet: ctor)
- Constr­uctors are methods that are called when an instance of a class is instan­tiated.
- Constr­uctors have no return type.
- Constr­uctors can be overloaded with different signat­ures.

public class Class­Name() // default constr­uctor
{ }

public class Class­Nam­e(int parameter) // overload
: this() //calls the default constr­uctor
{}

Functi­ons­/Me­thods

- Methods are the functions of classes.
- Methods can be overloaded with different signat­ures.

Decl­ara­tion: Access Parameter + Return Type + (Argument Type argument, ...) {}

Varying number of parame­ters:
public void Metho­dNa­me­(­par­ams int[] arguments) {}

Calling a functi­on/­met­hod:
retur­nValue = Funct­ion­Nam­e(­arg­ume­nts)
retu­rnValue = Object­Nam­e.­Me­tho­dNa­me­(ar­gum­ents)
 

Conven­tions

PascalCase
classes, methods, properties
camelCase
method parameters
_camelCase
private fields

Access Modifiers

public
accessible everywhere
private
accessible only from inside the class
prote­cted
internal
protected internal
Usable for classes, methods and variables.

Objects

var objec­tName = new Class­Nam­e();
Create an object
obje­ctN­ame­.­Met­hod­Nam­e()
Access public class methods
var x = Objec­tNa­me.P­ro­per­tyN­ame()
Gets an object property.
Obje­ctN­ame.Pr­ope­rty­Name = x
Sets an object property.