2nd Approach – Using the “Constructor” Constructor is a special type of method that is present in all classes, even if you don’t explicitly define it in the class itself. Constructors are used for creating an instance of the object…. this basically means that whenever you declare a new instance […]
Read More
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
So far we have looked at 5 ways to store data in a class’s object (aka an instance) in the form of “properties”. Of which one of the recommended ways of setting values to properties, is by using the constructor method, e.g.: [csharp] using System; using System.Collections.Generic; using System.Linq; using […]
Read More
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: [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InheritenceInAction { class Program { static void Main(string[] args) […]
Read More
Method Overloading is basically a technique that lets you have different versions of the same method, where each version of the method does a different thing. It essentially makes methods more versatile. Each version of the method accepts different/types of input parameters. The way you control which of version method […]
Read More
In the previous unit we came across: [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InheritenceInAction { class Program { static void Main(string[] args) { // Here we create an object using the orange class Orange AnOrange = new Orange(1.25, 45, "Spain", "25-01-2015", "sweet", true, "clementine"); […]
Read More
A variable is essentially a container that’s used for storing data. This container has a name assigned to it. In programming, this name is used to refer to the variable’s content. In c# there are actually three high-level types of variables: Value Types Reference types Pointer types (we will ignore […]
Read More
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: [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; […]
Read More
Previously we looked at why variables are categorized as either “value types”, or “reference types” (primarily for memory reasons). Now lets take a look at these variables in action, starting with the string variable. String variables We already encountered string variables when we created the “message” string variable in our […]
Read More
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: [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RandomNumberGenerator { class Program { […]
Read More