C# Difference between Class and Structure with Examples

  By : Suresh Dasari
  Posted On : 27-Apr-2023

Here, we will learn what is class in c#, uses of class, what is structure (struct) in c#, how to use structure in c#, and the differences between class and structure (struct) in c# with examples.

What is Class in C#?

In c#, class is a data structure that is useful to combine the various data members such as properties, constructors, methods, events, etc. into a single unit.

 

To define a class in c#, we need to use the class keyword. Following is the syntax of defining the class in c#.

 

public class User
{
   // properties, methods, events, etc.
}

In this class syntax, we used the class keyword to define the class in c# with public access modifier. Based on our requirements, we can use any access modifier along with the class. In c#, the access modifiers are useful to restrict the access of our class in applications.

C# Class Example

The following is an example of creating the class with various data members in c#.

 

public class User
{
   // Variables
   public int i = 0;
   public string msg = "Welcome to Tutlane";

   // Properties
   public string Name { get; set; }
   public string Location { get; set; }

   // Constructor
   public User()
   {
      // Constructor implementation
   }

   // Methods
   public void GetUserDetails()
   {
      Console.WriteLine(msg);
   }
}

To learn more about classes, visit Classes in C# with Examples.

What is Struct (Structure) in C#?

In c#, structure or struct is same as a class and it will contain various data members such as propertiesconstructorsmethodsevents, etc. The only difference between class and structure is class is a reference type and the structure is a value type. To learn about value type and reference type in c#, visit C# Value Type and References with Examples.

 

To define a structure, you need to use the struct keyword. Following is the syntax of defining the structure in c#.

 

public struct User
{
   // properties, methods, events, etc.
}

In this structure syntax, we defined a structure using the struct keyword along with the public access modifier. To learn about access modifiers, visit Access Modifiers in C# with Examples.

C# Structure (Struct) Example

The following is an example of defining the structure using the struct keyword in c#.

 

public struct User
{
   // Properties
   public string Name { get; set; }
   public string Location { get; set; }

   // Constructor 
   public User(string x, string y)
   {
      this.Name = x;
      this.Location = y;
   }

   // Methods
   public void GetUserDetails()
   {
      Console.WriteLine("Welcome to Tutlane");
   }
}

To learn more about structures, visit Structures in C# with Examples.

Differences between Class and Structure in C#

As discussed, both the class and structure are useful to combine various data members into a single unit. The following table lists the main differences between class and structure in c#.

 

ClassStructure
The class can be defined by using the class keyword. The structure can be defined by using the struct keyword.
In c#, classes are reference types. Structures are the value types.
In c#, we can instantiate the class using a new keyword. The structure can be instantiated with or without using a new keyword.
In c#, a class can inherit from other classes. The structure won't support inheriting from other structures or classes.
In c#, we can create a class with parameterless constructors. The structure will allow only constructors with parameters.
A class can implement one or more interfaces. The structure can implement only interfaces that contain no members.

To learn more about classes and structures in c#, visit our C# Tutorial.