Posts Tagged Under: tutorial

c# – Instantiate an object and set it’s Properties simultaneously (using the Constructors approach)

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 of a class, e.g.:

        Employee Dave = new Employee()

Then the constructor get’s executed, and in the above case, the only thing it does is set a side a memory container, waiting to be filled in. You can identify a constructor in a class by the fact that the constructor’s name must be the same as the class’s name itself. Hence in the case of the the “Employee” class, we didn’t explicitly declare the Employee constructor,


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:

  1. Program.cs – this is the main starting point of our component, and hence it contains the “Main” method to reflect this. All this will do is create a “Warehouse” object and then apply a non-static method to that object.
  2. Warehouse.cs – the warehouse class has a constructor, to make it easy to create an object using this class. It also has a method, which in turn instantiates an object belonging to the “item” class.
  3. Item.cs – this only returns

First let’s create a new project:

Notice the


c# – Create a read-only property (aka a constant)

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.:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Employee
{
    public string Firstname { get; set; }
    public int Age { get; set; }
    public double Salary { get; set; }

    public Employee(string firstname, int age, double salary)
    {

        Firstname = firstname;
        Age = age;
        Salary = salary;
  															

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:

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");
			string ContainsSeedsSentence;
            if (AnOrange.ContainsSeeds)
            {
     															

c# – Method Overloading: Have two or methods of the same name

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 you want to trigger, is by specifying the correct number/type of input parameters for that corresponding method.

In the following example, we have 2 methods, which in this case achieves the same goal by different means. The here is to instantiate an object:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Employee
{
    public string Firstname { get; set; }
    public int Age { get; set; }
    public double Salary { get;															

c# – Creating objects from generic parent classes

In the previous unit we came across:

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");
            string ContainsSeedsSentence;
            if (AnOrange.ContainsSeeds)
            {
  															

c# – What are Variables Types

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 this for now, but will cover it later)

Value Types

The following variable types fall under this category:

Type Represents Range Default
Value
bool Boolean value True or False False
byte 8-bit unsigned integer 0 to 255 0
char 16-bit Unicode character U +0000 to U +ffff ‘\0’
decimal 128-bit precise decimal values with 28-29 significant digits (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 0.0M
double 64-bit double-precision floating point type (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 0.0D
float 32-bit single-precision floating point type -3.4 x 1038 to + 3.4 x 1038 0.0F
int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0
long 64-bit signed integer type -923,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
sbyte 8-bit signed integer type -128 to

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:

 

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)
        {
            Orange AnOrange = new Orange(1.25, 45, "Spain", "25-01-2015", "sweet", true, "clementine");

            // The "SomeInfo" method hasn't been explicitly defined in the Orange class, 
			// Howeever the following method call															

c# – An overview of commonly used variables

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 hello world program.

You can also Concatenate existing string variables to form new string variables:

using System;
class ConcatenateStrings
{
	static void Main()
	{
		string firstname = "James" ;
		string lastname = "Bond" ;
		string fullname = firstname + " " + lastname;
		Console.WriteLine ("My name is " + lastname + ", " + fullname );

                string country = "uk";
            															

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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandomNumberGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
			Random RandomNumber = new Random();
			
			Console.WriteLine("Here is a list of random numbers:");
			Console.WriteLine(RandomNumber.Next());
			Console.WriteLine(RandomNumber.Next());
			Console.WriteLine(RandomNumber.Next());
			Console.WriteLine(RandomNumber.Next());
			Console.WriteLine(RandomNumber.Next());
			
		}
    }
	
}