Difference between structure and class in C#

In this tutorial, we will learn about Difference between structure and class with the help of examples in C#. By IncludeHelp Last updated : April 06, 2023

What is a class in C#?

A class is just like a blueprint or a model which is used to produce/create the objects. A class may contain multiple variables and methods to work on them.

What is a structure in C#?

A structure is a user-defined data type like the class. It mainly contains multiple variables, data types. A structure may also contain the fields and methods.

What are the differences between structure and class in C#

In C#, both class and structure are used to define a custom data type or a model that we can organize our needs with different types of variables and methods. Let's discuss some major differences between structure and class in C#.

  1. Classes are references types of data type, structures are value type of data type.
  2. Classes support default constructor i.e. we can set default values that will be assigned while creating an object. Structures do not support the concept of the default constructor, we cannot set values like classes that can be used as default values while creating a structure object/variable.
  3. Classes support the inheritance; structures do not support the inheritance.

C# example to demonstrate the differences between structure and class

In this example, we are creating a structure student_1 and a class student_2 along with the methods. To understand the difference between a class and structure in C#, please practice the given example.

using System;
using System.Text;

namespace Test {
  //structure 
  public struct student_1 {
    private string name;
    private short age;
    private float perc;

    //method
    public void setValue(string name, short age, float perc) {
      this.name = name;
      this.age = age;
      this.perc = perc;

    }
    public void dispValues() {
      Console.WriteLine("Name: {0}", name);
      Console.WriteLine("age: {0}", age);
      Console.WriteLine("perc: {0}", perc);
    }
  };

  //class
  public class student_2 {
    private string name;
    private short age;
    private float perc;

    //default constructor
    public student_2() {
      this.name = "N/A";
      age = 0;
      perc = 0.0f;
    }
    //method
    public void setValue(string name, short age, float perc) {
      this.name = name;
      this.age = age;
      this.perc = perc;

    }
    public void dispValues() {
      Console.WriteLine("Name: {0}", name);
      Console.WriteLine("age: {0}", age);
      Console.WriteLine("perc: {0}", perc);
    }
  };

  class Program {
    static void Main(string[] args) {
      //creating structure variable
      student_1 std1 = new student_1();
      //printing default values
      Console.WriteLine("std1 (default values)...");
      std1.dispValues();
      //setting values
      std1.setValue("Amit", 21, 98.23f);
      //printing after setting the values
      Console.WriteLine("std1 (after setting values)...");
      std1.dispValues();

      Console.WriteLine();

      //creating class object
      student_2 std2 = new student_2();
      //defaut constructor will be invoked
      //printing values which we set in default constructor
      Console.WriteLine("std2 (default values)...");
      std2.dispValues();
      //setting values
      std2.setValue("Amit", 21, 98.23f);
      //printing after setting the values
      Console.WriteLine("std2 (after setting values)...");
      std2.dispValues();

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

std1 (default values)...
Name:
age: 0
perc: 0
std1 (after setting values)...
Name: Amit
age: 21
perc: 98.23

std2 (default values)...
Name: N/A
age: 0
perc: 0
std2 (after setting values)...
Name: Amit
age: 21
perc: 98.23



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.