C# Data Types: Constants, Enumerations, Structures, and Arrays

Constants

Constants are immutable values that cannot be reassigned after initialization, whereas variables can be modified multiple times.

Declaration Syntax

const dataType constantName = value;

Use constants for values that should remain unchanged throughout the program, making maintenance easier when updates are needed.

Constants vs Static Variables

Both constants and static variables are initialized at compile time. Constants require explicit initialization and remain immutable throughout the program's execution. Static variables receive default values if not explicitly initialized and can be modified during runtime.

Constants can represent objects only if they are compile-time determinable values, such as string literals. Runtime-constructed objects cannot be assigned to constants.

Enumerations

Enumerations provide a structured way to define a set of named integral constants, ensuring consistent data representation.

Declaration Syntax

[public] enum EnumName
{
    Value1,
    Value2,
    // ...
}

Enumerations are typically declared at the namespace level for broad accessibility.

Basic Usage

public enum Weather { Spring, Summer, Autumn, Winter }

Weather current = Weather.Autumn;
Console.WriteLine(current);                    // Autumn
Console.WriteLine(Weather.Spring);            // Spring
Console.WriteLine((int)Weather.Spring);       // 0
Console.WriteLine((int)current);              // 2
Console.WriteLine(current + 1);               // Winter

Custom Values

public enum UserStatus
{
    Active = 1,
    Inactive,        // 2
    Away = 5,
    Busy,            // 6
    Offline          // 7
}

Type Conversions

Enum to Integer: Direct casting is supported

int code = 2;
Weather season = (Weather)code;
Console.WriteLine(season);  // Autumn

Enum to String: Use ToString() method

Console.WriteLine(Weather.Spring.ToString());  // Spring

String to Enum: Use Enum.Parse()

string input = "Summer";
Weather parsed = (Weather)Enum.Parse(typeof(Weather), input);

Structures

Structures enable grouping of related data elements of diffeernt types in to a single unit.

Declaration Syntax

[public] struct StructName
{
    public dataType fieldName;
    // ...
}

Example Implementation

public struct Employee
{
    public string firstName;
    public int employeeId;
    public Department department;
}

public enum Department
{
    Engineering,
    Marketing,
    Sales
}

Structures are value types allocated on the stack and support fields, properties, methods, and constructors. They are suitable for small data structures, while class are better for complex objects requiring reference semantics.

Arrays

Arrays store multiple elements of the same type in contiguous memory locations.

Declaration Syntax

dataType[] arrayName = new dataType[arrayLength];

Initialization Methods

int[] numbers1 = new int[5];                    // Recommended
int[] numbers2 = { 10, 20, 30, 40, 50 };       // Recommended
int[] numbers3 = new int[5] { 10, 20, 30, 40, 50 };
int[] numbers4 = new int[] { 10, 20, 30, 40, 50 };

Array Operations

Finding Extremes and Sum

int[] values = { 15, 8, 23, 4, 42, 16 };
int maximum = values[0];
int minimum = values[0];
int total = 0;

for (int index = 0; index < values.Length; index++)
{
    if (values[index] > maximum) maximum = values[index];
    if (values[index] < minimum) minimum = values[index];
    total += values[index];
}

Array Reversal

string[] words = { "C#", "Programming", "Language" };
string temporary;

for (int i = 0; i < words.Length / 2; i++)
{
    temporary = words[i];
    words[i] = words[words.Length - 1 - i];
    words[words.Length - 1 - i] = temporary;
}

Sorting Algorithms

// Manual sorting
int[] data = { 9, 5, 7, 1, 3 };
int swap;

for (int outer = 0; outer < data.Length - 1; outer++)
{
    for (int inner = outer + 1; inner < data.Length; inner++)
    {
        if (data[outer] > data[inner])
        {
            swap = data[outer];
            data[outer] = data[inner];
            data[inner] = swap;
        }
    }
}

// Using built-in methods
Array.Sort(data);      // Ascending order
Array.Reverse(data);   // Descending order

Methods

Methods encapsulate functionality for code reuse and organization.

Declaration Syntax

[public] static returnType MethodName([parameters])
{
    // Method implementation
}

The static modifier indicates that the method belongs to the type itself rather than to specific instances, enabling invocation without object instantiation.

Tags: C# constants Enums Structs Arrays

Posted on Sun, 17 May 2026 19:45:54 +0000 by yoma31