C# OOP

In this article, I will try to convey the basics of OOP in C# in the simplest way. The main purpose and benefits of OOP(C#) can be listed as follows;

  • Classes can be developed independently of each other.
  • You can split parts on the program.
  • You can grasp the logic of conquering the program very easily.

However, you can evaluate and handle each part of your program individually.

How to do this;


Button newbutton = new Button();
newbutton.Text = "Click me";
newbutton.Click += newbuttn_Click;
private void newbutton_Click(object sender, EventArgs e)
{
    Console.WriteLine("Hi n00b!");
}

You may not understand the lines of code above, don’t worry, I will cover them in a moment.

Advertisements

It would be good for us to know this too before moving on to the classes structure. Due to its OOP structure, it completely prevents code repetition (if used correctly). We should be aware that the benefits I mentioned make it easier to manage projects. From the largest project to the smallest projet, it will make them all feasible with much less effort.

I would simply define it for you as follows; If you want to make a button and use it, you first define the button class. Then you get samples from the button class wherever you want (known as instance). There is a Button class defined in the .NET Framework. If you want, instead of making a definition, you can derive an object directly from the class in the library (you can take an instance).


Let’s check the example I gave above now.

Button newbutton = new Button();

newbutton.Text = "Click me";
newbutton.Click += newbuttn_Click;

private void newbutton_Click(object sender, EventArgs e)
{
    Console.WriteLine("Hi n00b!");
}

In the first line, an example is taken from the Button class. Look at the second line, “Click me” text is assigned to the Text property of the Button object named “newbutton” created. In the third line, the new button. Click + = is typed and when the Tab key is pressed on the keyboard, newbutton_Click is written opposite the equals. Pressing the Tab key once more will create the code block that will run when the button below is clicked.

In this way, the need for the development of the button and the effects of this development on the application have been eliminated, and the project has been managed more comfortably.

Example C# classes story;

For example, we have iron. The brand, model, color, electrical voltage of the iron, what kind of fabrics it can iron are the features (data) of this iron. We can also heat our iron, use it for ironing and let it cool. These are the functions of the iron (method). If we had made a program about ironing and used the object-oriented programming technique, we would immediately create an ironing class. In this class, there was information (data) about ironing and the tasks (method) we could do with ironing. Then in object-oriented programming, there are data belonging to the class and functions/methods that process this data and produce useful results.


If you need more information on the subject, you can check this document prepared by Microsoft. Document



Remember that we can make mistakes too, the important thing is to correct these mistakes. If you have any ideas that you see as a mistake, you can let us know by filling out the form below.

Samet Salih
Samet Salih

Junior Software Developer
I’m working on C# and Python

Liked it? Take a second to support Selim on Patreon!
Advertisements

Selim

Backend Developer, Öğrenci, Blogger

You may also like...

Yorum yap

%d blogcu bunu beğendi: