filmov
tv
enum in C# | #MortalTech | #10
![preview_player](https://i.ytimg.com/vi/Cw5NTIiFQ8w/maxresdefault.jpg)
Показать описание
You can also find me here:
In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when referring to the day in a week.
An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays.
Example: Define an Enum
enum WeekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Above, the WeekDays enum declares members in each line separated by a comma.
Enum Values
If values are not assigned to enum members, then the compiler will assign integer values to each member starting with zero by default. The first member of an enum will be 0, and the value of each successive enum member is increased by 1.
Example: Default Enum Values
enum WeekDays
{
Monday, // 0
Tuesday, // 1
Wednesday, // 2
Thursday, // 3
Friday, // 4
Saturday, // 5
Sunday // 6
}
You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.
Example: Assign Values to Enum Members
enum Categories
{
Electronics, // 0
Food, // 1
Automotive = 6, // 6
Arts, // 7
BeautyCare, // 8
Fashion // 9
}
You can even assign different values to each member.
Example: Assign Values to Enum Members
enum Categories
{
Electronics = 1,
Food = 5,
Automotive = 6,
Arts = 10,
BeautyCare = 11,
Fashion = 15,
WomanFashion = 15
}
The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.
Specify the type after enum name as : type. The following defines the byte enum.
Example: byte Enum
enum Categories: byte
{
Electronics = 1,
Food = 5,
Automotive = 6,
Arts = 10,
BeautyCare = 11,
Fashion = 15
}
Instagram: MortalsTech
Facebook: MortalTech
Twitter: Mortalstech
In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when referring to the day in a week.
An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays.
Example: Define an Enum
enum WeekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Above, the WeekDays enum declares members in each line separated by a comma.
Enum Values
If values are not assigned to enum members, then the compiler will assign integer values to each member starting with zero by default. The first member of an enum will be 0, and the value of each successive enum member is increased by 1.
Example: Default Enum Values
enum WeekDays
{
Monday, // 0
Tuesday, // 1
Wednesday, // 2
Thursday, // 3
Friday, // 4
Saturday, // 5
Sunday // 6
}
You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.
Example: Assign Values to Enum Members
enum Categories
{
Electronics, // 0
Food, // 1
Automotive = 6, // 6
Arts, // 7
BeautyCare, // 8
Fashion // 9
}
You can even assign different values to each member.
Example: Assign Values to Enum Members
enum Categories
{
Electronics = 1,
Food = 5,
Automotive = 6,
Arts = 10,
BeautyCare = 11,
Fashion = 15,
WomanFashion = 15
}
The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.
Specify the type after enum name as : type. The following defines the byte enum.
Example: byte Enum
enum Categories: byte
{
Electronics = 1,
Food = 5,
Automotive = 6,
Arts = 10,
BeautyCare = 11,
Fashion = 15
}
Instagram: MortalsTech
Facebook: MortalTech
Twitter: Mortalstech