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# – 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# – 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# – Instantiate an object and set it’s Properties simultaneously (using the Initialization approach)

 

1st Approach: Using Initialization

Here we use properties (as demonstrated in the previous), but this time we set all the properties in one go, by writing them as part of the object’s actual declaration, using curly-bracket syntax:

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;}
}

class Program
{
    static void Main()
    {
        Employee Dave = new Employee()
		{
			Firstname = "David",
			Age = 35,
			Salary = 35235.23
		};
		// Using the above syntax is called initialisation

        Console.WriteLine("{0} is {1} years old and earns  £{2} per year.", Dave.Firstname, Dave.Age.ToString(), Dave.Salary.ToString());
    }
}

c# – Storing data inside objects (by using “Properties”)

Previously we looked at how to store data in an object using fields. Fields are easy to use, but only if you set them to public. Having public fields is bad practice.

To overcome this we can set the fields to private and then write (public) methods within the class to access/modify the fields. This approach keeps the fields private, but unfortunately it is a much more messier solution….that requires writing a lot more code, and is more harder to follow.

That’s where “properties” comes in to save the day. Properties, gives you the best of both worlds, i.e.:

  • It keeps the object’s data private
  • It keeps the syntax simple and has minimal overhead in terms of number of lines.

Properties are type of class member that is specifically used for storing an object’s


c# – Storing data inside objects (by using “fields”)

Whenever you create an object (aka an instance of a class), you often want to store data within that object. You can think of this info as the object’s metadata. For example, lets say want to have a class called “Employee”, and you want to use that class to create “employee” objects, and each employee-object needs to store the following info:

firstname
age
DateJoined
Salary

Note: the words “instance” and “object” are synonyms.

In order to make this possible, you first need to write your Employee class to accept this info. After that, you can call on the class to instantiate the object and pass the metadata into it.

There are a number of ways to write your

1st Approach: Store the data directly in an object, using “Fields”

In this approach we have declared inside


c# – Methods, Classes, and Namespaces

When programming it is essential to organize and structure your code, otherwise it will become unwieldy to work with.

c# offer a number of ways to structure and and organize our code using:

  • methods
  • classes
  • namespaces

Methods are used for storing blocks of codes and each method is designed to perform a specific task (aka function).

Methods, in turn, are stored in “classes”. These classes class can store one or more methods.

Classes in turn are stored in “namespaces”. You can think of Namespaces as a bit like folders on a windows machine. That means that namespaces have tree structures where a namespace can house classes as well as other lower-level namespaces. As a result we have the following hierarchy:

namespaces => classes => methods

One of the key strength of c# is that it has


c# – Adding comments to your code

It is always good practice to insert comments into your code so that you (as well as others) can understand what your code is doing.

You can place a single line comment after a line of code, or on a line of it’s own using the “//” notation.

If you want to insert a multi-line comment, then you can begin it using /* and end the comment with */.

Here we the 2 comments types in actions

/*
Program name: Hello World
Version:      1.0
Description:  Opens up a terminal and displays "Hello World"
*/

using System; // here we are importing the "System" namespace

// We are about to define the HelloWorld class.
class HelloWorld // This line is called the "class declaration" line
{     															

c# – Hello World

Here we are going to first show you a simple “hello world” program in the form of an exe file. When you double-click on the exe file, all that will happen is that a console terminal will open a display the message “hello world”, like this:

 

Step 1 -Write the code

Open notepad and write:


using System;
class HelloWorld
{
   static void Main()         [content_tooltip id="650"]
     {
        string message = "Hello World";
        Console.WriteLine (message);
        Console.ReadLine();
     }
}

[vision_notification style=”warning” font_size=”12px” closeable=”false”] In c#, everything is case sensitve. That means if


c# – What you should already know

You should already be familiar with the following:

  • Variables
  • Conditonal construct, e.g. If statements
  • Conditional Operators,
  • regular expressions
  • wild cards