Content

  • Methods
  • Method Parameters
  • Method Overloading
  • OOP
  • Classes and Objects
  • Class Members
  • Constructors
  • Access Modifiers
  • Properties (Get and Set)

Methods

  • A method is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a method.
  • Methods are used to perform certain actions, and they are also known as functions.
  • Why use methods? To reuse code: define the code once, and use it many times.
  • A method is defined with the name of the method, followed by parentheses (). C# provides some pre-defined methods, such as Main(), but we can also create our own methods to perform certain actions

Example :

static void MyMethod() 
{
    Console.WriteLine("I just got executed!");
}
            
static void Main(string[] args)
{
    MyMethod();
}
  • MyMethod() is the name of the method
  • static means that the method belongs to the Program class and not an object of the Program class.
  • void means that this method does not have a return value.
  • In the following example, MyMethod() is used to print a text (the action), when it is called

Method Parameters

  • Information can be passed to methods as parameter. Parameters act as variables inside the method.
  • You can add as many parameters as you want, just separate them with a comma.

Example :

static void MyMethod(string fname,string lname) 
{
    Console.WriteLine(fname + " " + lname);
}
    
static void Main(string[] args)
{
    MyMethod("ABC","PQR");
}

Method Overloading

With method overloading, multiple methods can have the same name with different parameters

Example

static int PlusMethod(int x, int y)
{
    return x + y;
}
    
static double PlusMethod(double x, double y)
{
    return x + y;
}
    
static void Main(string[] args)
{
    int myNum1 = PlusMethod(8, 5);
    double myNum2 = PlusMethod(4.3, 6.26);
    Console.WriteLine("Int: " + myNum1);
    Console.WriteLine("Double: " + myNum2);
}

OOP

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

Object-oriented programming has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time

Classes and Objects

Everything in C# is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

Example

//file1.cs
using System;

namespace MyApplication
{
    class Program
    {
    static void Main(string[] args)
    {
        Car myObj = new Car();
        Console.WriteLine(myObj.color);
    }
    }
}

//file2.cs
using System;

namespace MyApplication
{
  class Car
  {
    public string color = "red";
  }
}

Class Members

Fields and methods inside classes are often referred to as "Class Members"

Example

// The class
class MyClass
{
    // Class members
    string color = "red";        // field
    int maxSpeed = 200;          // field
    public void fullThrottle()   // method
    {
    Console.WriteLine("The car is going as fast as it can!");
    }
}

Constructors

A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It can be used to set initial values for fields

// Create a Car class
class Car
{
    public string model;  // Create a field
    
    // Create a class constructor for the Car class
    public Car()
    {
    model = "Mustang"; // Set the initial value for model
    }
    
    static void Main(string[] args)
    {
    Car Ford = new Car();  // Create an object of the Car Class (this will call the constructor)
    Console.WriteLine(Ford.model);  // Print the value of model
    }
}
    
// Outputs "Mustang"

Access Modifiers

Access modifier is used to set the access level/visibility for classes, fields, methods and properties.

Modifier Description
public The code is accessible for all classes
private The code is only accessible within the same class
protected The code is accessible within the same class, or in a class that is inherited from that class.
internal The code is only accessible within its own assembly, but not from another assembly.

Encapsulation & Properties (Get and Set)

The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must

  • declare fields/variables as private
  • provide public get and set methods, through properties, to access and update the value of a private field

private variables can only be accessed within the same class (an outside class has no access to it). However, sometimes we need to access them - and it can be done with properties.

A property is like a combination of a variable and a method, and it has two methods: a get and a set method

//file1.cs
using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      Person myObj = new Person();
      myObj.Name = "Liam";
      Console.WriteLine(myObj.Name);
    }
  }
}

//file2.cs
using System;

namespace MyApplication
{
  class Person
  {
    private string name;  // field
    public string Name    // property
    {
      get { return name; }
      set { name = value; }
    }  
  }
}