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.

Anatomy: Name, Fields, Methods

Declar­ation:
Access­Mod­ifier + class + ClassName


Constr­uctors: (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 ClassName() // default constr­uctor

{ }


public class ClassName(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.

Declar­ation:
Access Parameter + Return Type + (Argument Type argument, ...) {}


Varying number of parameters:
public void MethodName(params int[] arguments) {}


Calling a functi­on/­method:
return­Value = Functi­onName(argum­ents)

return­Value = ObjectName.MethodName(argum­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
protected
internal
protected internal
Usable for classes, methods and variables.

Objects

var objectName = new ClassName();
Create an object
objectName.Method­Name()
Access public class methods
var x = Object­Nam­e.P­rop­ert­yName()
Gets an object property.
Object­Nam­e.P­rop­ert­yName = x
Sets an object property.