Quantcast
Channel: 2,000 Things You Should Know About C# » Enum
Browsing all 13 articles
Browse latest View live

Image may be NSFW.
Clik here to view.

#367 – Iterating through All Possible Values of an Enumeration Type

There are occasions when it’s desirable to iterate through all possible values of an enumeration type.  You can do this using the static Enum.GetValues method. Suppose that we have an enumeration type...

View Article



Image may be NSFW.
Clik here to view.

#457 – Converting Between enums and their Underlying Type

When you declare an enum, by default each enumerated value is represented internally with an int.  (System.Int32 – 4 bytes).  You can convert between values of the underlying type and enum values using...

View Article

Image may be NSFW.
Clik here to view.

#458 – Errors While Converting Between enum and Underlying Type

You can convert to an enum value from its underlying type by casting the underlying type (e.g. int) to the enum type. However, when you cast a value that doesn’t have a corresponding enumerator in the...

View Article

Image may be NSFW.
Clik here to view.

#459 – Assigning a Value of a Different Type to an enum

You can convert to an enum value from its underlying type by casting the underlying type (e.g. int) to the enum type.  You can also assign a value of a different type, one that does not match the...

View Article

Image may be NSFW.
Clik here to view.

#460 – Converting from a String to an Enum Type

You can convert a string to the corresponding enumerator in an enum type by using the Enum.Parse method.  This is a static method that takes the specific enumerated type and a string value and returns...

View Article


Image may be NSFW.
Clik here to view.

#461 – Enumeration Elements Don’t Need to Be Sequential

Enumeration elements are implicitly set to consecutive integers, starting at 0, as indicated in the comments below. // Default type is int public enum Mood { Crabby, // 0 Happy, // 1 Petulant, // 2...

View Article

Image may be NSFW.
Clik here to view.

#462 – Duplicate Enumerators in an Enumerated Type

When defining a new enumerated type, you can define multiple enumerators that have the same underlying value.  You might do this when you have a finite set of enumerated values and you want to map them...

View Article

Image may be NSFW.
Clik here to view.

#463 – Enumerated Values Can Be Any Constant Expression

When you define an enum, the individual values can be implicitly defined (one greater than the previous value), assigned to a constant, or assigned to any constant expression. In the definition of the...

View Article


Image may be NSFW.
Clik here to view.

#464 – Getting an Enumeration’s Underlying Type at Runtime

You can use the Enum.GetUnderlyingType static method to find out what the underlying type is that is being used to store an enumerated type’s enumerated values. public enum Moods : byte { NOMOOD = 0,...

View Article


Image may be NSFW.
Clik here to view.

#626 – Nested Type Options

When you declare one type inside of another, the outer type must be either a class or a struct.  The inner (nested) type can be one of the following: class, struct, interface, delegate or enum. Here...

View Article

Image may be NSFW.
Clik here to view.

#982 – An Enum Type Can Store a Maximum of 32 Flags

When you store a boolean value in a bool type, each boolean value uses 1 byte, or 8 bits–the size of a bool instance. You can be more efficient in storing boolean values by using each bit within a...

View Article

Image may be NSFW.
Clik here to view.

#1,025 – Converting between enum Types

You can convert between two different enum types as long as they both use the same underlying type (e.g. int).  You use an explicit cast to do the conversion. Assuming two enum types that by default...

View Article

Image may be NSFW.
Clik here to view.

#1,026 – Checking a Flagged enum Type for Validity

You normally use the Enum.IsDefined method to check whether a particular enum value is a valid value.  This method does not work on flagged enums. Assuming the following enum type: [Flags] public enum...

View Article

Browsing all 13 articles
Browse latest View live




Latest Images