C# Tutorial class and objects


What is a class. The short answer is here but it’s not satisfying most people wanted to learn modern software development. In past (70’s and 80’s) there were procedural languages such C. The computer executed the program line by line, e.g.:

  1. Let initialize variable A=10
  2. Print the variable A to the screen
  3. Increment the A+A+1
  4. Go to line 2

It’s very simple example. Nowadays we have Object Oriented programming languages called “OO” such C++, C#, Java and other languages.

The best way to explain what the class is provide an example. The class is a template and holds information about variables and methods (I’m going to explain what are variables and methods in other chapters).

Let suppose we want to develop the software which will keep information about patients and blood tests.

Below is our example of Patient class:

1
2
3
4
5
6
public class Patient { 
  private string name; 
 
  public Patient() { 
  } 
}

The class holds information about name of the patient. One of the purpose of the class is to hold variables and methods.  Below is the example of the same class Patient but with added one method ‘setName’ which will set the patient’s name.

 

1
2
3
4
5
6
7
8
9
10
public class Patient { 
  private string name; 
 
  public Patient() { 
  } 
 
  public void setName(string name){
    this.name = name;
  }
}

The method ‘setName’ inside the class set value of the variable ‘name’. Below is a small demo how we can use our class in real application.

 

1
2
3
4
5
6
7
8
9
10
11
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Patient newPatient = new Patient();
            newPatient.setName("John");
        }
    }
}

In the example above we created an instance of object named ‘newPatient’ from class ‘Patient’. What it means? The class is a template and an object is a real instance. We can have multiple instances of objects from this call (or any other classes). We can write:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Patient newPatient1 = new Patient();
            newPatient1.setName("John");
 
            Patient newPatient2 = new Patient();
            newPatient2.setName("Sam");
        }
    }
}

In scenario above we created two objects of call ‘Patient’. Each of the object is a separate instance of class.

Now we can add more details to the class Patient. Let’s add an age of the patient.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Patient { 
  private string name; 
  private int age;
 
  public Patient() { 
  } 
 
  public void setName(string name){
    this.name = name;
  }
 
  public void setAge(int age){
    this.age = age;
  }
}

And now we can set the age for each of the patient. The class definition contains keywords such: private, public.

private keyword means that property, e.g. name can be only modified inside the class. This is why I used ‘setter’ method which allows change the name of the patient. The code below throws an error during compilation:

 

1
2
3
4
5
6
7
8
9
10
11
12
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Patient newPatient = new Patient();
            newPatient.name = "John"; //(Error)
            newPatient.setName("John"); //(OK)
        }
    }
}

As mentioned above the setter method allows change the value of property ‘name’. Why? Because the class definition contains declaration: public void setName(string name). 

Public – means that the property/method can be accessed outside the class. In our example above, the line ‘newPatient.setName(“John”);

When we want to call a method, we use syntax: object.method or object.property.

In examples above there are keyword ‘void’. The ‘void’ keyword means (in short definition) that particular method doesn’t returns any value. If we add new method (getName) to the class Patient:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Patient { 
  private string name; 
  private int age;
 
  public Patient() { 
  } 
 
  public void setName(string name){
    this.name = name;
  }
 
  public string getName(){
    return name;
  }
 
  public void setAge(int age){
    this.age = age;
  }
}

We can get name of the patient from our object. The new method ‘getName’ has a keyword ‘string’ before method name. It’s mean that the method return value of property ‘name’.

 

Please watch my c# for beginners course

https://sylwestermadej.teachable.com/p/csharp-development-for-beginners