C# Enum - c# - c# tutorial - c# net



C# Enum

Learn c# - c# tutorial - C# Enum - c# examples - c# programs

What is Enum in C# ?

  • Enum in C# is also known as enumeration. It is used to store a set of named constants such as season, days, month, size etc.
  • The enum constants are also known as enumerators. Enum in C# can be declared within or outside class and structs.
  • learn csharp tutorial - c# Data Strcutures - c# example programs

    learn csharp tutorial - c# Data Structures - c# example programs

  • Enum constants has default values which starts from 0 and incremented to one by one. But we can change the default value.
 enum in C#

enum in C#

Points to remember

  • enum has fixed set of constants
  • enum improves type safety
  • enum can be traversed

C# Enum Example

  • Let's see a simple example of C# enum.
using System;  

public class EnumExample  
{  
    public enum Season { WINTER, SPRING, SUMMER, FALL }    
  
    public static void Main()  
    {  
        int x = (int)Season.WINTER;  
        int y = (int)Season.SUMMER;  
        Console.WriteLine("WINTER = {0}", x);  
        Console.WriteLine("SUMMER = {0}", y);  
    }  
}
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

WINTER = 0
SUMMER = 2
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# enum example changing start index

using System;  

public class EnumExample  
{  
    public enum Season { WINTER=10, SPRING, SUMMER, FALL }    
  
    public static void Main()  
    {  
        int x = (int)Season.WINTER;  

        int y = (int)Season.SUMMER;  

        Console.WriteLine("WINTER = {0}", x);  

        Console.WriteLine("SUMMER = {0}", y);  
    }  
} 
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

WINTER = 10
SUMMER = 12

C# enum example for Days

using System;  

public class EnumExample  
{  
    public enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };  
  
    public static void Main()  
    {  
        int x = (int)Days.Sun;  

        int y = (int)Days.Mon; 
 
        int z = (int)Days.Sat;

        Console.WriteLine("Sun = {0}", x);
  
        Console.WriteLine("Mon = {0}", y);
  
        Console.WriteLine("Sat = {0}", z);  
    }  
}
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

Sun = 0
Mon = 1
Sat  = 6

C# enum example: traversing all values using getNames()

using System;  

public class EnumExample   
{  
    public enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };  
  
    public static void Main()  
{  

       foreach (string s in Enum.GetNames(typeof(Days)))  
        {  
            Console.WriteLine(s);  
        }  
    }  
}
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

Sun
Mon
Tue
Wed
Thu
Fri
Sat
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# enum example: traversing all values using getValues()

using System;  

public class EnumExample  
{  

    public enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };  
  
    public static void Main()  
{  

        foreach (Days d in Enum.GetValues(typeof(Days)))  
       
 {  
            Console.WriteLine(d);  
        }  
    }  
}
click below button to copy the code. By - c# tutorial - team

C# examples - Output :

Sun
Mon
Tue
Wed
Thu
Fri
Sat


Related Searches to C# Enum