Difference between structure and class in C#

C# structure and class contrasts: Here, we will realize what are the contrasts among structures and classes in C#?

C# class and structure

In C# and other programming dialects, structure and classes are utilized to characterize a custom information type, that we can sort out as indicated by our need with various kinds of factors, techniques and so forth.

Both are not the equivalent. Here, we are composing contrasts among structure and class, they have the accompanying fundamental contrasts…

Contrasts between C# classes and C# Structures

  1. Classes are references sorts of information type, structures are esteem kind of information type.
  2. Classes bolster default constructor, for example, we can set default esteems that will be doled out while making an item. Structures don’t bolster the idea of the default constructor, we can’t set qualities like classes that can be utilized as default esteems while making a structure object/variable.
  3. Classes bolster the legacy; structures don’t bolster the legacy.

Example:

In this example, we are making a structure student_1 and a class student_2 alongside the techniques. To comprehend the contrast between a class and structure in C#, if you don’t mind 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

Leave a Comment

error: Alert: Content is protected!!