Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are user defined data types in C#?
User-defined data types in C# allow developers to create custom types beyond the built-in primitive types. The main user-defined data types in C# are structures, enumerations, classes, interfaces, and delegates. This article focuses on structures and enumerations as fundamental user-defined types.
Structure
In C#, a structure is a value type data type that helps you group related data of various types into a single unit. The struct keyword is used for creating a structure.
Syntax
public struct StructName {
// fields, properties, methods, constructors
public dataType field1;
public dataType field2;
public StructName(parameters) {
// constructor implementation
}
}
Key Features of Structures
- 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.
Example
using System;
public struct Point {
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void Display() {
Console.WriteLine("Point: ({0}, {1})", x, y);
}
public double Distance(Point other) {
return Math.Sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
}
public class Program {
public static void Main() {
Point p1 = new Point(3, 4);
Point p2 = new Point(6, 8);
p1.Display();
p2.Display();
Console.WriteLine("Distance between points: {0:F2}", p1.Distance(p2));
}
}
The output of the above code is −
Point: (3, 4) Point: (6, 8) Distance between points: 5.00
Enumeration
An enumeration (enum) is a user-defined data type that consists of a set of named constants. Enums are used to store related constants like days of the week, months, seasons, etc.
Syntax
public enum EnumName {
Constant1,
Constant2,
Constant3
}
The default value of enum constants starts from 0 and increments by 1. You can also assign custom values −
public enum Priority {
Low = 1,
Medium = 2,
High = 3
}
Example with Default Values
using System;
public enum Vehicle {
Car,
Bus,
Truck
}
public class Program {
public static void Main() {
Vehicle v1 = Vehicle.Car;
Vehicle v2 = Vehicle.Bus;
Console.WriteLine("Vehicle: {0}, Value: {1}", v1, (int)v1);
Console.WriteLine("Vehicle: {0}, Value: {1}", v2, (int)v2);
// Iterating through all enum values
Console.WriteLine("\nAll vehicles:");
foreach (Vehicle vehicle in Enum.GetValues(typeof(Vehicle))) {
Console.WriteLine("{0} = {1}", vehicle, (int)vehicle);
}
}
}
The output of the above code is −
Vehicle: Car, Value: 0 Vehicle: Bus, Value: 1 All vehicles: Car = 0 Bus = 1 Truck = 2
Example with Custom Values
using System;
public enum Status {
Inactive = 0,
Active = 1,
Pending = 5,
Suspended = 10
}
public class Program {
public static void Main() {
Status currentStatus = Status.Active;
Console.WriteLine("Current Status: {0}", currentStatus);
Console.WriteLine("Status Value: {0}", (int)currentStatus);
// Converting from int to enum
Status newStatus = (Status)10;
Console.WriteLine("Status from value 10: {0}", newStatus);
// Check if a value is defined in enum
Console.WriteLine("Is value 3 defined? {0}", Enum.IsDefined(typeof(Status), 3));
Console.WriteLine("Is value 5 defined? {0}", Enum.IsDefined(typeof(Status), 5));
}
}
The output of the above code is −
Current Status: Active Status Value: 1 Status from value 10: Suspended Is value 3 defined? False Is value 5 defined? True
Comparison
| Feature | Structure | Enumeration |
|---|---|---|
| Type | Value type | Value type |
| Purpose | Group related data of different types | Define named constants |
| Memory | Stack allocation | Stack allocation |
| Inheritance | Cannot inherit from other types | Cannot inherit |
| Default Values | Zero/null for all fields | First constant (usually 0) |
Conclusion
User-defined data types in C# provide powerful ways to organize and structure data. Structures are ideal for grouping related data as value types, while enumerations create readable named constants. Both are value types stored on the stack, making them efficient for certain scenarios.
