C# tutorial – Object-oriented programming and basic C# syntax 1

Objects

facebooktwittergoogle_pluslinkedinmail

Concept of object-oriented programming

C# is a simple, general-purpose, modern, object-oriented programming language. I know, It’s a lot of technical terms in one sentence. Let me explain meaning of each:

  • simple – C# is easy to understand and learn
  • general-purpose – C# will do fine for most programming challenges
  • modern – All fancy new stuff can be implemented in C#
  • object-oriented – C# supports object-oriented paradigm

Let’s focus a bit on the last point. You might hear many various definitions about object-oriented programming, but there is one very simple. Object-oriented programming is about modelling real world objects or concepts as objects in an application:

  • An object is something – a person
  • An object has data – a person has a name
  • An object performs actions – a person can introduce itself

Ok, we know what an object is, but what else? Object-oriented programming supports:

  • Encapsulation – Object can hide it’s private data from other objects.
    Example: A person hides it’s wallet.
  • Inheritance – One object can be inherited from a different one.
    Example: Employee is inherited from Person. Employee has name data as Person has. Employee can have more data than Person (i.e. Employer name).
  • Polymorphism – It’s a difficult one to explain, but let’s try. If an object is inherited from a base object it can act as the base object as well.
    Example: An Employee object is inherited from Person, this way Employee object might be threaded as a Person object – Employee can introduce itself as Persons do, but it can do it differently. Let’s assume each Person introduce itself by saying ‘My name is X’. Employee could introduce itself by saying ‘I’m X and I work for Y’.

Objects in ‘C# Hello World’ example

Before an object is created it needs to be defined. Let’s get back to C# Hello World example from previous post. You will find a class definition in the example.

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

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

This class definition consist of:

  • class  keyword
  • Name – ‘Program’ in this case
  • Class body (inside curly brackets) – class data and methods (a method performs an action)

The Program class has no data and only one method called Main.

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

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

This method declaration consist of:

  • static  keyword – it’s a method modifier (I’ll do a separate post about modifiers)
  • Method signature
    • Return object type – void  is a special keyword that tells that nothing will be returned
    • Name – ‘Main’
    • Parameters (inside angle brackets)
  • Method body (inside curly brackets) – a set of instructions to be run when method is executed

When an application starts .NET framework searches all classes for an entry point – a specific method signature. Then the entry point is the first code that is executed. The Main method meets entry point specification, therefore it’s the place where your program starts.

Let’s go to your first code.

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

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

It takes the Console object. Then the .  (dot) allows you to access available Console elements – WriteLine method. Finally in brackets parameters are passed for WriteLine method – a string (text data type). Strings are always placed inside double quotes ( ) in C#.

Note that it’s very useful to have self-explanatory names: Console.WriteLine  method just writes line of text in the console window.

facebooktwittergoogle_pluslinkedinmail

Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “C# tutorial – Object-oriented programming and basic C# syntax