Object Oriented Programming Style in C# (OOP)

Introduction:

This tutorial is on how to use OOP (Object Oriented Programming) in C#.

What is OOP?

OOP is when you are able to run multiple instances of one class or form at one time without them colliding in to one another. I will give you an example; Let's say that we have a 'person' class, with the variables to hold that person's name and age. Without OOP, we could only have one person. But, with OOP, we could create multiple instances of this 'Person' class, and give them all their own names/ages.

Class:

First create a new C# project and create a new class named 'Person.cs'. In that class, create a variable to hold their name...
  1. string name = string.Empty;
And another one for the age;
  1. int age = -1;
We write default values as false ones because then we can tell if something isn't setting the variables correctly later on.

Form:

Now that we have our class with our variables, we are going to go back to our Form1/Main.cs file of the project we just created and create two instances of the 'Person.cs' class. We do this the exact same way as any other object or variable, we first write the data type 'Person' followed by the object/variable name, then set it equal to a new instance of the class, like so...
  1. Person personA = new Person();
  2. Person personB = new Person();
Next we are going to set some details for each of them. Set personA's name to 'Josh' and age to '21'...
  1. personA.name = "Josh";
  2. personA.age = 21;
Now, we have no errors. Next we are going to set some personB details...
  1. personB.name = "Derick";
  2. personB.age = 45;

Output:

Finally we are going to output the details for each of them to show that they are using the same class, but not conflicting variables...
  1. MessageBox.Show(personA.name + " " + personB.name + Environment.NewLine + personA.age.ToString() + " " + personB.age.ToString());

Finished!

Full source code... Main.cs/Form1:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace JayPabsOOP
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void Form1_Load(object sender, EventArgs e)
  21. {
  22. Person personA = new Person();
  23. Person personB = new Person();
  24. personA.name = "Josh";
  25. personA.age = 21;
  26. personB.name = "Derick";
  27. personB.age = 45;
  28. MessageBox.Show(personA.name + " " + personB.name + Environment.NewLine + personA.age.ToString() + " " + personB.age.ToString());
  29. }
  30. }
  31. }
Person.cs:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace JayPabsOOP
  8. {
  9. class Person
  10. {
  11. public string name = string.Empty;
  12. public int age = -1;
  13. }
  14. }

Add new comment