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#, Class is a data structure, and it will combine various types of data members such as fields, properties, member functions, and events into a single unit.
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.
If you observe the above image, we used various data members like access modifiers, fields, properties, methods, constructors, etc., in our c# class based on our requirements.
We will learn more about c# access modifiers, fields, properties, methods, constructors, etc., topics in the following chapters with examples.
As discussed, a class can contain multiple data members in the c# programming language. The following table lists different types of data members that can be used in c# classes.
Member | Description |
---|---|
Fields | Variables of the class. |
Methods | Computations and actions that can be performed by the class. |
Properties | Actions associated with reading and writing are 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 are supported by the class. |
Constants | Constant values are 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 are 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 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.
The above example shows that we created a new class called “Users” with the required data members and member functions. To access Users class methods and properties, we created an object (user) for the Users class and performed the required operations.
When we execute the above c# program, we will get the result below.
This is how we can create and use classes and objects in c# applications based on our requirements.