// C# program to illustrate the enums// with their default valuesusingSystem;namespaceConsoleApplication1 {// making an enumerator 'month' enummonth{ // following are the data members jan, feb, mar, apr, may}classProgram { // Main MethodstaticvoidMain(string[] args) { // getting the integer values of data members..Console.WriteLine("The value of jan in month "+"enum is "+ (int)month.jan); }}}
from enum import Enum
class Season(Enum):
SPRING = 1
SUMMER = 2
AUTUMN = 3
WINTER = 4
for season in (Season):
print(season.value,"-",season)
enum Days {
SUNDAY("Day of Rest"),
MONDAY("Start of Work Week"),
TUESDAY("Second Day of Work Week"),
WEDNESDAY("Mid Week"),
THURSDAY("Almost There"),
FRIDAY("End of Work Week"),
SATURDAY("Weekend Fun");
// ฟิลด์สำหรับเก็บคำอธิบาย
private String description;
// คอนสตรัคเตอร์ของ enum
Days(String description) {
this.description = description;
}
// เมธอดสำหรับการเรียกใช้ฟิลด์ description
public String getDescription() {
return this.description;
}
// เมธอดอื่นๆ ที่สามารถเพิ่มได้
public void printDay() {
System.out.println(this.name() + ": " + this.getDescription());
}
}
public class Main {
public static void main(String[] args) {
// เรียกใช้งาน enum และเมธอด
Days today = Days.MONDAY;
// เรียกใช้เมธอด getDescription
System.out.println(today.getDescription()); // Output: Start of Work Week
// เรียกใช้เมธอด printDay
today.printDay(); // Output: MONDAY: Start of Work Week
}
}
enum Months
{
January, // 0
February, // 1
March=5, // 5
April, // 6
May, // 7
June, // 8
November=May+4, // 11
December // 12
}
static void Main(string[] args)
{
int myNum1 = (int) Months.April;
Console.WriteLine(myNum1);
int myNum2 = (int) Months.November;
Console.WriteLine(myNum2);
}
from enum import Enum
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
# การเข้าถึงค่าสมาชิก Enum
print(Color.RED
print(Color.RED.name)
print(Color.RED.value)
// C# program to illustrate the changing
// of data type of enum members
using System;
namespace ConsoleApplication4 {
// changing the type to byte using :
enum Button : byte {
// OFF denotes the Button is
// switched Off... with value 0
OFF,
// ON denotes the Button is
// switched on.. with value 1
ON
}
class Program {
// Main Method
static void Main(string[] args)
{
Console.WriteLine("Enter 0 or 1 to know the " +
"state of electric switch!");
byte i = Convert.ToByte(Console.ReadLine());
if (i == (byte)Button.OFF)
{
Console.WriteLine("The electric switch is Off");
}
else if (i == (byte)Button.ON)
{
Console.WriteLine("The electric switch is ON");
}
else
{
Console.WriteLine("byte cannot hold such" +
" large value");
}
}
}
}
input
1
output
Enter 0 or 1 to know the state of electric switch!
The electric switch is ON