What are the differences between a class and a structure in C#?


Structure

In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.

To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program.

Classes

When you define a class, you define a blueprint for a data type. A class definition starts with the keyword class followed by the class name, and the class body enclosed by a pair of curly braces.

Structure vs Classes

  • Structures are value types and the classes are reference types.

  • On passing a class to a method, it is passed by reference. But when a structure is passed to a method, it is passed by value.

  • Classes supports Inheritance but this is not the case with structure.

  • Destructor in a class can be used but in Structure, you cannot use a Destructor.

  • Structures cannot have default constructor

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements