C# Intermediate Tutorial – OOP

Welcome to the first lesson of the C # intermediate level, this course will be the most important lesson of the intermediate level.

In this section, we will learn about classes, objects and the concept of OOP. We will also repeat old topics in this lesson.

List Of Topics To Be Covered

  • Object-Oriented Programming In C#
  • Classes And Objects
  • Class Members

Object-Oriented Programming In C#

We will use all the concepts here in all future lessons, so let’s try to get a good grasp of them. First, we will summarize the topics and then repeat them with a diagram.

What Is OOP?

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.

Advantage OOP
  • OOP is fast and easy
  • OOP provides a clear structure for the programs
  • OOP is facilitates debugging and organizes code
  • OOP is It reduces the writing time of the code and improves performance.
What Are Classes And Objects?

You can think of classes as a general category and objects are the parts within these categories. Every object has a class, but not every class has an object.

Now let’s solve examples to make class and object distinction easier, you can increase the number of examples.

Class -> Car
Objects -> Ford, Toyota, BMW

Class -> Food
Objects -> Pizza, Hamburger, Chicken

Class -> Computer
Objects -> MSI, DELL

As you can see, each class is like a box containing objects, and then we will use these two concepts in C #.

Classes And Objects

Now we will see how a class is created and how objects are generated from the class. We will use 2 different methods to create it.

public class Player
{
  public string name = "Player";
  public int damage = 100;
}

This block of code represents a class, you can either write it in a new file or use it in the main file. First, let’s examine the codes in the main file before learning how to use it.

class ProjectName
{
  public static void Main(String[] args)
  {
     // Code
  }
}

As you can see, C # needs a main class to run a project, and there is one main method in the main class. Now let’s use the class we created in the project.

public static void Main(String[] args)
{
   // Code
}

public class Player
{
  public string name = "Player";
  public int damage = 100;
}

We have created a class that is not included in the main class anymore. We can use this class by creating an object in the main. Let’s create an object in the main class.

class Program
{
  public static void  Main(String[] args)
  {
    Player player = new Player();
  }
}

We derive the player object from the Player class, then synchronize this object to the Player class. After creating a new object here, we can use the properties of the other class with this object.

Note: You can derive as many objects from a class as you want, there is no limit to it, you can derive objects repeatedly with the above method.

Create Class In New File

Create another file other than the main file, name it and end with .cs then put your class in this file and derive your object from the class in the same way.

File1:
class Program
{
   public static void Main(String[] args)
   {
      Player player = new Player();
   }
}
File2:
public class Player
{
   public string name = "Player";
   public int damageAmount = 100;
}

Class Members

In this section, we will see calling variables and methods in classes. We have been pulling the WriteLine function from the Console class all the time so far, we will now pull our variables from the class we created.

// Class File

public class Player
{
   public string name = "Player";
   public int damageAmount = 100;
}
// File
class Program
{
   public static void Main(String[] args)
   {
      Player player = new Player();
      Console.WriteLine(player.name);
   }
}
Output: Player

As you can see, after specifying the class, you can access the variable you want by specifying the variable name.

Calling Methods In Class

You can also access the methods you created in the class like variables. Now let’s create a method in the Player class and call it in the main file.

// Main File

class Program
{
   public static void Main(String[] args)
   {
      Player player = new Player();
      player.Attack(20);
   }
}
// Class File

public class Player
{
   public string name = "Player";
   public int damageAmount = 100;
   
   public int Attack(int damage)
   {
      return damage;
   }
}
Output: 20

Leave a Reply

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