articles

Home / DeveloperSection / Articles / Class and Struct in C#

Class and Struct in C#

Sushant Mishra5087 04-Mar-2017
class 

Class is a basic of oops concept. Class is like a blue print of objects. In real world object has attributes, like color, shape etc. and also have functionalities like walking, eating, etc. Class contains both attribute and functionalities of a set of objects. For example an employee class have name, salary, designation and department. If any person that meets these requirements is the object of Employee class. A class enables you to create your own custom type of objects using variables or field, methods, constructor, events etc.

Class is reference type, it means using functionality of class cam access through only objects. We can define accessibility of using access specifiers. You can inherit one class to another class.

Struct

In c# struct include a value type entity is called as Struct. A structure also contains constructor, constants, fields, methods, properties, indexers, operators, events and nested types. Structure is a value type, it means it store on stack location. 

A struct can be initialized using with or without new keyword same as primitive variable or objects. 


using System;
struct Books
{
    public string title;
    public string author;
    public string subject;
    public int book_id;
};
public class testStructure
{
    public static void Main(string[] args)
    {
        Books Book1;   /* Declare Book1 of type Book */
        Books Book2;   /* Declare Book2 of type Book */
        /* book 1 specification */
        Book1.title = "C Programming";
        Book1.author = "Balagurswami";
        Book1.subject = "C Programming Tutorial";
        Book1.book_id = 6495407;
        /* book 2 specification */
        Book2.title = "Telecom Billing";
        Book2.author = "Zara Ali";
        Book2.subject = "Telecom Billing Tutorial";
        Book2.book_id = 6495700;
        /* print Book1 info */
        Console.WriteLine("Book 1 title : {0}", Book1.title);
        Console.WriteLine("Book 1 author : {0}", Book1.author);
        Console.WriteLine("Book 1 subject : {0}", Book1.subject);
        Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);
        /* print Book2 info */
        Console.WriteLine("Book 2 title : {0}", Book2.title);
        Console.WriteLine("Book 2 author : {0}", Book2.author);
        Console.WriteLine("Book 2 subject : {0}", Book2.subject);
        Console.WriteLine("Book 2 book_id : {0}", Book2.book_id);
        Console.ReadLine();
    }
}
Difference between Struct and Class:

1. Class is Reference type but struct is Value type.

2. Struct cannot declare a default constructor and destructor. Struct can define

parameterized constructor.

3. Struct can initialize without new keyword but you can't able to use its

methods,indexer or events. So you must initialize struct for using its functionalities.

4.Using struct you can't derive another class or struct and also cannot use it as a

base class.


You can visit these related articles and post

Difference between class and structure.

Abstract class in C#

Encapsulation in C# and OOPs



Updated 28-Nov-2017

Leave Comment

Comments

Liked By