In c#, Classes and Objects are interrelated. The class in c# is nothing but a collection of various data members (fields, properties, etc.) and member functions. The object in c# is an instance of a class to access the defined properties and methods.
We will now learn the classes and objects in c# and how to use them in c# applications with examples.
In c#, classes are declared by using
class keyword. Following is the declaration of class in c# programming language.
If you observe the above syntax, we defined a class “users” using
class keyword with
public access modifier. Here, the
public access specifier will allow the users to create objects for this class, and inside of the body class, we can create required fields, properties, methods, and events to use in our applications.
Now we will see how to create a class in c# programming language with example.
Following is the example of creating a class in c# programming language with various data members and member functions.
If you observe the above c# class example, we defined a class “Users” with various data members and member functions based on our requirements.
Following is the detailed description of various data members used in the above c# class example.
As discussed, a class can contain multiple data members in the c# programming language. The following table lists a different type of data members that can be used in c# classes.
|Fields||Variables of the class|
|Methods||Computations and actions that can be performed by the class|
|Properties||Actions associated with reading and writing named properties of the class|
|Events||Notifications that can be generated by the class|
|Constructors||Actions required to initialize instances of the class or the class itself|
|Operators||Conversions and expression operators supported by the class|
|Constants||Constant values associated with the class|
|Indexers||Actions associated with indexing instances of the class like an array|
|Finalizers||Actions to perform before instances of the class are permanently discarded|
|Types||Nested types declared by the class|
We can use the required data members while creating a class in c# programming language based on our requirements.
In c#, Object is an instance of a class that can be used to access the data members and member functions of a class.
Generally, we can say that objects are the concrete entities of classes. In c#, we can create objects by using a new keyword followed by the class's name like as shown below.
Users user = new Users();
If you observe the above example, we created an instance (user) for the class (Users), which we created in the previous section. Now the instance “user” is a reference to an object that is based on Users. Using the object name “user” we can access all the data members and member functions of the Users class.
Following is the example of creating objects in the c# programming language.
If you observe the above example, we created a new class called “Users” with a required data members and member functions. To access Users class methods and properties, we created an object (user) for the Users class and performing the required operations.
When we execute the above c# program, we will get the result as shown below.
This is how we can create and use classes and objects in c# applications based on our requirements.