What is a structure in C#?

CsharpProgrammingServer Side Programming

A structure in C# 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.

C# structures have the following features −

  • Structures can have methods, fields, indexers, properties, operator methods, and events.

  • Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.

  • Unlike classes, structures cannot inherit other structures or classes.

  • Structures cannot be used as a base for other structures or classes.

  • A structure can implement one or more interfaces.

Let us see how to define a structure −

struct Student {
   public string name;
   public int id;
   public string subject;

Above we have a structure with student details. The details will come under different datatypes i.e. string for name, int for id, etc.

Updated on 21-Jun-2020 13:03:43