C# is the best recommended language among all OOPS Languages. In C#, classes support polymorphism, inheritance and also provide the concept of derived classes and base classes. A class is nothing but an encapsulation of properties and methods which might be used to represent a real-time entity.
Among all types of Object-oriented programming languages, C# is the best recommended improved OOPS language for a complete enhanced Programming career. C# language endows with complete support for object-oriented programming which includes EPI (encapsulation, polymorphism, and inheritance).
Encapsulation refers to a group of relevant properties, methods, and other programming members that are considered as a single unit or object. By using Encapsulation, we can use a unit of the object for various programming purposes.
Polymorphism is meant for having multiple classes that can be incorporated interchangeably, even though every class applies similar properties or methods in diversified approaches. Throughout Polymorphism method, you don’t have to use different properties or methods for different classes.
Inheritance explains the capability to make new classes according to an existing class and hence, creating multiple classes is easier through inheritance method.
The terms class and object are used to describe the specific type of objects, and the occurrence of classes, respectively. So, we call the task of creating an object as instantiation. Throughout the blueprint analogy, a class is considered as a blueprint, and an object is a creation made from the class blueprint. Hope, now you must have clarified the relationship between class and blueprint.
For defining a class you need to use: ‘class SampleClass { }’
When you don't desire any assistance for the polymorphism or inheritance, C# provides useful types called structures.
For defining a structure, we use code:
Each class can have different class members with appropriate properties to explain the class data, appropriate methods to explain the class behavior, and appropriate events that allow communication between various classes and objects.
Fields and properties stand for information that an object possesses truly. Fields are similar to variables as they can be read or set straightly, subject to appropriate access modifiers.
For defining a field that is accessible from within any instances of the class, we use code
The properties of a class contain ‘get and set’ accessors to put more control on setting or returning of values. In C# you will be able to create a private field to store the property value or use auto-applied properties that create this field robotically and provide the basic logic for the property process.
For defining an auto-applied property, we use code
If you want to execute some added operations for reading and writing of the value of the property, first define a field to store the value of the property and give the basic logic to store and retrieve the same as follows:
Most of the properties possess some methods to set and get the value of property value. On the other hand, you can set read-only or write-only properties to control them from being updated or view. In C#, you can also skip the get or set procedure of property. Nevertheless, the properties that are auto-implemented cannot be write-only and the auto-implemented properties that are Read-only can be set in builders of the carrying class.
A method can be simply explained as an action that an object can truly perform.
Following are the codes for defining a class method:
There are a number of applications, or overrides in a class for the same method that varies in the number of parameters or parameter types.
To override a method
In most cases, you declare a method within a class definition. However, C# also supports extension methods that allow you to add methods to an existing class outside the actual definition of the class.
Constructors are the methods of a class that are performed automatically when a given type of object is created. Generally, the constructors declare the data members of a new object, and a constructor can run only for one time when a class is created. In addition, the constructor code often runs before the other code in a class. On the other hand, you can make multiple constructors override in the same process as for any other procedure.
Following are the codes for defining a class constructor:
A finalizer is often used to destruct class instances and clean up the unmanaged sources. Especially in .NET, the garbage collector functions as a finalizer which automatically handles the provision and discharge of memory for the controlled objects in your C# application.
Events help a class or object to inform other classes or objects when an interesting event happens. The class that raises the event is known as the publisher and the classes that receive the event are subscribers. Overall, the event is dependent upon the Publishers and subscribers.
The event keyword is used to declare an event in a class and call upon the event delegate to create an event.
Now, use the += operator to subscribe to an event and use the -= operator, to unsubscribe from an event.
When a class is defined within another class is known as a nested class and the nested class is declared as private. Find following codes:
In order to create a the nested class instance, you need to use the container class name, dot(.), and the name of the nested class. See the codes:
All classes and members of the class can state their level of access to other classes throughout the access modifiers in C#.
C# Access Modifier and Definition
Instantiating classes
First, create an instance of the class to create an object, and then you can give values to the properties of instance and fields and call on class methods.
Find following codes:
To set property value at the time of the class instantiation method, use object initializers:
A static class member is a property, method, or field that is shared by all class instances. In C#, the static classes contain only static members and hence cannot have an instance. Static members are also unable to access non-static fields, properties, or methods. But, for accessing the static member, use the class name without creating an object of the same class:
Following are the codes for defining a static member:
Anonymous types help you to create objects exclusive of writing the definition of a class for the type of data. Ultimately, the compiler creates a class for you as an option. This anonymous class has no proper usable name and carries the properties you mention in defining the object.
Following are the codes for the creation of an anonymous type instance:
Hope the above session must help you out understand the class and objects along with file, properties, and events when you work on an interface. You can easily pass the .Net interviews in C# language with the help of the above codes.