Below is my object orientated programming (C# in particular) cheat sheet, can be useful for interviews etc. It isn’t a full guide, but a good guide to start with and may help you score that dream programming job. I will add to it from time to time, so make sure to keep checking back for updates etc.
Default access modifier
The default access modifier changes per scenario, and they are as follows:
top level class: internal
method: private
members (unless an interface or enum): private (including nested classes)
members (of interface or enum): public
constructor: private (note that if no constructor is explicitly defined, a public default constructor will be automatically defined)
delegate: internal
interface: internal
explicitly implemented interface member: public
Access modifiers
public – you can access this from anywhere within the current assembly and any assembly that references that assembly
private – can only access from the same class itself
internal – can be accessed from only the assembly that is it located in
protected – similar to private, but can only be accessed from the class itself or any derived class from it
protected internal – same as internal, but can also be seen by any derived classes in other assemblies
Derived classes
They are classed that are based on class (e.g. a base class). They inherit the base classes methods and properties
Base classes
They are the master classes of derived classes
Ref and Out
ref – this is initiated outside of the method (objects e.g. Person { Name, Surname } are ref by default). They are two way references, it will set the value of the value passed in. A value can be passed into a ref
out – this is initiated inside of the function and will return to the variable the value that is set within the function (nothing can be passed in though to this variable). This value has to be set.
Abstract classes
A base class that is basically a set of methods and properties for a starting point for a class to inherit from, the class can add more methods and properties to the class and also can override virtual methods
An abstract function can have no functionality. You’re basically saying, any child class MUST give their own version of this method, however it’s too general to even try to implement in the parent class.
A virtual function, is basically saying look, here’s the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override me, and provide your own functionality.
Marked as virtual are methods that include code that be overridden
Marked as abstract can’t include code and must be overridden
Not marked are normal and can just be used
Interfaces
Interfaces are like blueprints, these are methods / properties that a class needs to have in it.
Methods inside an interface are public by default, you cannot change them.
Static keyword
You can’t create an instance of a static class, or e.g. any static methods are not included in a instance of a class.
You can’t inherit from a static class.
A static class means that all methods / properties within the class have to be static and means the class can’t be implemented e.g. with = new Class();
Static classes are useful for e.g. helper methods that don’t need to interact with anything within the class etc. E.g. converting an integer to a string.
Objects
An object is an instance of a class e.g. Car _car = new Car();
OOP Basics
Abstraction – only expose what needs to be exposed, only show relevant and essential data
Polymorphism – Allowing you to use an entity in multiple forms
Encapsulation – Prevents the data from unwanted access by binding of code and data in a single unit called object.
Inheritance – Inheriting code from parent / base classes. Prevents duplicating code.
Sealed keyword
Use this to prevent a class from being inherited any further
Method Overloading
Using the same method, passing in different parameters
Method Overriding
Overriding a method in a derived class from a base class.. e.g. abstract class
Virtual
To override a method you need to mark it as virtual
Delegates
A delegate is passing a method into another method
Namespace
Namespace is considered as a container that contains functionally related group of classes and other types.
Hashtable
A hashtable is an object that holds key / value pairs
Constructors
Implementations of objects.. e.g.
public class Person {
public Person() {
// code
}
public Person(int number) {
// code
}
}
Code to be run on implementation of an object, you can overload with various parameters also
Destructors
Similar to constructors, used for garbage collection of objects. Called the same name as the class and proceed with a tilda ~
Class derive
Classes derrive from System.Object
Errors
- Syntax error. This type of error, which is identified during compilation, occurs because the programmer has used syntax incorrectly or included a typo in the code.
- Logic error. This type of error causes the program to do something other than what the programmer intended. The program will output an unexpected result in response to tests.
- Runtime error. This type of error causes the program to crash or terminate incorrectly.
Serialization
Serialization is a way of converting an object into a byte of data e.g. a stream of data of a file (xml etc) data.
The easiest way to think of it is in the terms of XML, say you have an object of type Person and to serialize that object you would be converting that object to an xml file to save on the file system.
De-serialization means to take that xml document and convert it back to a type of Person again.
This can be handy if you get xml files to import into a website and want to convert the files into an easy readable object.
Volatile Keyword
This is used to not cache a variable
No Comments