Home » C#.Net

Difference between structure and class in C#

C# structure and class differences: Here, we are going to learn what are the differences between structures and classes in C#?
Submitted by IncludeHelp, on February 12, 2019

C# class and structure

In C# and other programming languages, structure and classes are used to define a custom data type, that we can organize according to our need with different types of variables, methods etc.

Both are not the same. Here, we are writing differences between structure and class, they have the following basic differences...

Differences between C# classes and C# Structures

  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.

Example:

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

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.