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

CsharpProgrammingServer Side Programming

Class

Class is a blueprint for a data type. A class definition starts with the keyword class followed by the class name.

Struct

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.

The following are the differences −

  • Classes are reference types and structs are value types

  • Structures do not support inheritance

  • Structures cannot have default constructor

  • When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator.

  • Structures do not support inheritance

  • Structures cannot have default constructor

raja
Published on 14-Aug-2018 08:53:13
Advertisements