C# Classes and Objects with Examples

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.

C# Class

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.

Declaring a Class in C#

In c#, classes are declared by using class keyword. Following is the declaration of class in c# programming language.

 

public class users {

// Properties, Methods, Events, etc.

}

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, propertiesmethods, and events to use in our applications.

 

Now we will see how to create a class in c# programming language with example.

C# Class Example

Following is the example of creating a class in c# programming language with various data members and member functions.

 

public class Users
{
     public int id = 0;
     public string name = string.Empty;
     public Users()
     {
        // Constructor Statements
     }
     public void GetUserDetails(int uid, string uname)
     {
         id = uid;
         uname = name;
         Console.WriteLine("Id: {0}, Name: {1}", id, name);
     }
     public int Designation { get; set; }
     public string Location { get; set; }
}

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.

 

C# Class Example Detailed Description Diagram

 

If you observe the above image, we used various data members like access modifiersfieldspropertiesmethods, constructors, etc., in our c# class based on our requirements.

 

We will learn more about c# access modifiersfieldspropertiesmethodsconstructors, etc., topics in the following chapters with examples.

C# Class Members

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.

 

MemberDescription
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.

C# Object

In c#, Object is an instance of a class that can be used to access the data members and member functions of a class.

Creating Objects in C#

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.

C# Objects Example

Following is the example of creating objects in the c# programming language.

 

using System;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
          Users user = new Users("Suresh Dasari", 30);
          user.GetUserDetails();
          Console.WriteLine("Press Enter Key to Exit..");
          Console.ReadLine();
      }
   }
   public class Users
   {
       public string Name { get; set; }
       public int Age { get; set; }
       public Users(string name, int age)
       {
           Name = name;
           Age = age;
       }
       public void GetUserDetails()
       {
           Console.WriteLine("Name: {0}, Age: {1}", Name, Age);
       }
   }
}

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.

 

C# Classes and Objects Example Result

 

This is how we can create and use classes and objects in c# applications based on our requirements.