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.

 

Now we will learn what are the classes and objects in c# and how to use it in c# applications with examples.

C# Class

In c#, Class is a data structure and it will combine a 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 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 an objects for this class and inside of the body class, we can create a required fieldspropertiesmethods and events to use it 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 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 which we used in above c# class example.

 

C# Class Example Detailed Description Diagram

 

If you observe above image, we used a 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 next chapters with examples.

C# Class Members

As discussed, a class can contain multiple data members in c# programming language. Following table lists a different type 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 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 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 and 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 an objects by using a new keyword followed by the name of the class like as shown below.

 

Users user = new Users();

If you observe above example, we created an instance (user) for the class (Users) which we created in previous section. Now the instance “user” is a reference to an object that is based on Users. By using object name “user” we can access all the data members and member functions of Users class.

C# Objects Example

Following is the example of creating an objects in 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);

        }

    }

}

If you observe 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 Users class and performing required operations.

 

When we execute above c# program, we will get the result like as shown 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.