Understanding the basic fundamental of C# is very important. Classes and Objects are the terms we often use. Hope this blog will help you a lot to understand both terms.
All Object oriented languages are not the same. Some languages are procedural, some are object oriented and others are procedural incorporating some features of object oriented. C# however is a strict object oriented languages. It is quite similar to java and C++. C# stores its data and functions in the Objects. C# is not like JavaScript that keep its variables global. In C# everything is belonged to something and its variables are not global. Fields, methods and objects all are the part of classes and structures
Class
A class is something that create methods ,variables ,events etc. Here is the example of C# code that contains Class. Classes are declared using word Class and then followed by the name of Class like in the example below the name is “program” followed by keyword Class
C# The Base Class Library
C# has some basic built in libraries or frameworks which contains many helpful classes like Math’s file Management. E.g. Common mathematics class is “ System.maths” from which you can implement round off operations, PI values and many more.
Member Functions and Encapsulation
Member functions are the functions declared within the class and can be accessed through the object. These member functions and member variables can be kept private through encapsulation. C# supports the following access through encapsulation:
- Public
- Private
- Protected
- Internal
- Protected internal
Inherited classes
We can also have some Custom Classes in which we can have our own methods. One of the important concepts of classes is inheritance. Inheritance means taking some methods from base class instead of declaring again and again. In C# only Single inheritance is allowed in real world example “one child have one father, he cannot have multiple fathers”. But a class can implement multiple interfaces
E.g The following example shows Inheritance in classes
Syntax Derived Class: Base Class
None: Class Doremon{} // Dore mon is a robot and he has no father
Single Class Nobita: Nobita mom{} // Nobita has some qualities resemble to his mother
None, implements two interfaces Class Jiaan Dad: Jaiko ,Jiaan {} // Jaiko and Jiaan are siblings have some qualities similar to their father
Objects
You are travelling in a desert and finding a pitcher to drink water. You found a pitcher that is empty. Oh GOD ! there is no water, no use. You cannot remove your thirst unless there is water in it:(
Similar is the case with classes. Classes are the dummy pitchers no of use until and unless its objects are made. Objects defines their behavior. We use objects to call their methods or member functions. Most of the time we have to deal with the instance of classes. These instances are the objects. Objects are defined in classes and structures. That’s why everything is an object in C#.
E.g.
Nobita quality1=new Nobita(“black Hair”,10);
System.out.println("Nobita has: " + quality1.getQualities());
In this example “Nobita” is a class whereas quality1 is the reference to an object which is passing parameters to the constructor of “Nobita” class and will display the qualities by calling a member function getQualities().