Both class and structure are used to define custom data types and encapsulation of data members but some difference between them.
Inheritance: in structure we can not u inherit other structure but in class can inherit other classes.
Reference type vs Value type : Class is reference type means when a object of class is created, a reference of object is created.
changes to the value reflected whenever a reference is used.
Structure is Value type means a object of structure created with value that save in memory and changes to variable not reflected anywhere.
Default Constructor : a class always have default constructor but structure does not have default constructor
Size and Performance : Structure are small in size than classes because they does not have reference variables, that means structure can be faster than classes.
Class Example
public class Author {
// Data members of class
public string name;
public string language;
public int article_no;
// Method of class
public void Details(string name, string language,
int article_no)
{
this.name = name;
this.language = language;
this.article_no = article_no;
Console.WriteLine("The name of the author is : " + name
+ "\nThe name of language is : " + language
+ "\nTotal number of article published "
+ article_no + "\nTotal number of Improvements:");
}
// Main Method
public static void Main(String[] args)
{
// Creating object
Author obj = new Author();
// Calling method of class
// using class object
obj.Details("Sachin", "C#", 80, 50);
}
}
Structure Example
public struct Car
{
public string Brand;
public string Model;
public string Color;
}
class GFG {
// Main Method
static void Main(string[] args)
{
// Declare c1 of type Car
// no need to create an
// instance using 'new' keyword
Car c1;
// c1's data
c1.Brand = "Bugatti";
c1.Model = "Bugatti Veyron EB 16.4";
c1.Color = "Gray";
// Displaying the values
Console.WriteLine("Name of brand: " + c1.Brand
+ "\nModel name: " + c1.Model
+ "\nColor of car: " + c1.Color);
}
}