C# is a fully Object oriented programming language “OOP”, the first word is object, an object is something that can be seen, felt, or touched; something that has physical existence in the real world, if an item is virtual, this means that it doesn’t have any physical existence and is not considered an object. The second word is oriented, which indicates a direction or something to aim for, for example, when we say that we are oriented toward the building, we mean that we are facing towards it.
The third word is programming. Programming is just giving instructions to the computer. As the computer doesn’t speak our language, we humans have to give instructions to the computer in a language that the computer understands. We humans call these instructions computer programs, as we are guiding or instructing a computer to do a particular thing.
OOP means that we write our computer programs by keeping objects at the center of our thinking. OOP is neither a tool nor a programming language it is just a concept. Some programming languages are designed to follow this concept. C# is one of the most popular OOP languages. There are other object oriented languages, such as Java, C++, and so on.
In OOP, we try to think about our software components as small objects, and create relationships between them to solve a problem.
In OOP, you derive objects from classes. Classes are one of the most important concepts in OOP, you can say they are the building blocks of OOP. A class can be described as the blueprint of an object. A class is like a template or blueprint that tells us what properties and behaviors an instance of this class will have. In most circumstances, a class itself can’t actually do anything, it is just used to create objects.
An object is an instance of a class. In other words, an object is an implementation of a class. For example, in a banking application, we have a Customer class, but that doesn’t mean that we actually have a customer in our application. To create a customer, we have to create an object of the Customer class.
A variable is something that varies, which means it is not constant. In programming, when we create a variable, the computer actually allocates a space in memory for it so that a value of the variable can be stored there.
A method is a piece of code that is written in the code file and can be reused. A method can hold many lines of code, which will be executed when it is called. Let’s take a look at the general form of a method:
The first thing in the method declaration is an access_modifier, this will set the access permission of the method. Then, we have the return_type of the method, which will hold the type that the method will return, such as string, int, double, or another type. After that, we have the method name and then brackets, which indicate that it is a method. In the brackets, we have the parameter list, this can either be empty or can contain one or more parameters. Finally, we have curly brackets, which hold the method body, the code that the method will execute goes inside here. Any code following this structure will be considered a method by the C# compiler.
In every class, there is a special type of method, called a constructor. You can create a constructor in a class and program it, if you don’t create one yourself, the compiler will create a very simple constructor and use that instead. A constructor is a method that gets triggered when an object of a class is created. A constructor is mainly used to set the prerequisites of the class. A constructor doesn’t have a return type. This is because a constructor can’t return anything; it’s for initialization, not for any other type of action. Normally, the type of access is public for constructors, because otherwise no object can be instantiated. If you specifically want to prevent objects of a class from being instantiated, you can set the constructor as private. Another interesting thing is that you can have multiple constructors in a class. You might have one constructor that takes one argument and another that doesn’t take any arguments. Depending on the way in which you are initializing the object, the respective constructor will be called.
In the next example, show the use of classes, objects, variables and constructors.