What are user defined data types in C#?


The User defined data types in C# are structures and enumeration.

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.

The 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.
  • Structure members cannot be specified as abstract, virtual, or protected.

Enumeration

Enum is Enumeration to store a set of named constants like year, product, month, season, etc.

The default value of Enum constants starts from 0 and increments. It has fixed set of constants and can be traversed easily.

Let us see an example.

We have set the enum like this −

public enum Vehicle { Car, Bus, Truck }

Updated on: 20-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements