Partager via


Using Enumerations 

The Visual J# compiler allows you to use enumerations defined in the .NET Framework or other user-defined assemblies. Enumerations are treated as reference types by default. Enumerations can be cast to and from their underlying primitive types. They can also be assigned to Object, in which case the compiler implicitly performs the necessary boxing.

Example

import System.*;
public class MyClass
{
   public static void main() 
   {
      DayOfWeek friday = DayOfWeek.Friday;

      // Call a method that has a parameter of type DayOfWeek
      TestClass mySchedule = new TestClass();
      mySchedule.SetNonWorkingDay(friday);
      // convert an enum to its underlying primitive:
      int i = (int) friday; 
      Console.WriteLine(i);

      // OK to cast a primitive type to enum:
      DayOfWeek monday = (DayOfWeek) 1;
      Console.WriteLine(monday);

      // following line automatically boxes enum friday
      // to System.Object:
      System.Object obj = friday;
      Console.WriteLine(obj);
   }
}

public class TestClass
{
   public void SetNonWorkingDay(DayOfWeek dow)
   {
   }
}

Output

5
Monday
Friday

Applying bitwise operators (|, &, ^) to enumeration types is allowed. The following code performs a bitwise-OR operation on System.Windows.Forms.AnchorStyles in a call to CheckBox.set_Anchor:

CheckBox.set_Anchor(AnchorStyles.Top | AnchorStyles.Bottom)

See Also

Reference

enum Keyword
Syntax for Targeting the .NET Framework
Property and Event Exposure