Home >Backend Development >C#.Net Tutorial >In C#, what is a structure?

In C#, what is a structure?

王林
王林forward
2023-08-27 18:29:021203browse

In C#, what is a structure?

In C#, a structure is a value type data type. It helps you to make a single variable hold related data of different data types. Use the struct keyword to create structures.

C# structures have the following characteristics:

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

  • A structure can have a defined constructor, but no destructor. 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 the basis for other structures or classes.

  • A structure can implement one or more interfaces.

Let’s see how to define a structure:

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

Above we have a structure that contains student details. These details will belong to different data types, such as strings for names, integers for student IDs, and so on.

The above is the detailed content of In C#, what is a structure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete