c# – Arrays

Arrays are used to store a a group of items as a collection: Tip: In the above examples we used the for-loop to process each item in the variable. However, it is often easier to use the foreach loop instead (we will do this in our next example). Arrays can […]Read more »

c# – An intro to Arrays and Generics

Here are some terminologies: Arrays: This is a object variable, that is designed to store a collection of object-variable. Each object variable is stored in numbered containers (i.e. they are indexed), with the first container starting at number 0. Generics: these are like arrays, but they can only store builtin […]Read more »

c# – Random Number Generator

Here’s a quick a simple class for creating a a series of random numbers, by creating an instance of the “Random” class, and applying this class’s next() method: http://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx Here is the code:

c# – Over-riding generic methods

Child classes can inherit methods from it’s parent class, in the same way that it inherits properties. Here is an example of the Orange class inheriting the “SomeInfo” method, which have been originally defined in the Item class:     However you can make some changes to the above code […]Read more »

c# – Creating objects from generic parent classes

In the previous unit we came across: In C# you can create an object using the parent class, but instantiate it with a child classe’s constructor method, e.g.: This means that the object is technically an “item” object and not an “orange” object. However you can convert high level object […]Read more »

c# – Inheritence in Action

Lets say we have a supermarket and it sells a different types of fruits, then you could create a class for each type of fruit: The above will output: The resulting output would be: Now there is a good chance that this supermarket sells a lot of different types of […]Read more »

c# – A practical example of OOP in action

Tip: it is best practice to only define one class per cs file. Tip: For consistency, and to make life easier, the cs file’s name should be named after the class’s name. Let’s say we have a warehouse ordering system…which we breakdown into 3 cs files: Program.cs – this is […]Read more »

c# – The 5 Pillars of Object Oriented Programming

There are 5 concepts of OOP: Encapsulation Data Hiding Specialization Polymorphism Division of Responsibility     Encapsulation This means that you can use a class without the need to view the code inside the class. All you have knowledge of the class, are it’s exposed members. Data Hiding A class’s […]Read more »

c# – The “this” keyword

Earlier we cam across the following: If you look at the above code, you will notice that the employee class’s constructor has input parameters that are called Firstname, Salary, and Age. But these names are also used as the names of the Employee class’s properties. As a result it can […]Read more »

c# – Declare variables outside if-else statement scope

If you declare (and initialize) a variable within an if-else statement, then that variable’s value will only exist within that if-else statement. However if you want to initialize (i.e. assign a value to) the variable within the if-else statement, but want to make the variable accessible outside the if-else scope, […]Read more »