Home |  JournalSeek |  SoftwareSeek |  GenomeSeek |  Expression |  Developer |  TakeOnIt
Return to Genamics Home

6. Enums

In case you don't know C, Enums let you specify a group of objects, e.g.:

Declaration:

public enum Direction {North, East, West, South};

Usage:

Direction wall = Direction.North;

It's a nice construct, so perhaps the question is not why did C# decide to have them, but rather, why did Java choose to omit them? In Java, you would have to go:

Declaration:

public class Direction {
	public final static int NORTH = 1;
	public final static int EAST = 2;
	public final static int WEST = 3;
	public final static int SOUTH = 4;
}

Usage:

int wall = Direction.NORTH;

Despite the fact the Java version seems to express more, it doesn't, and is less type-safe, by allowing you to accidentally assign wall to any int value without the compiler complaining. To be fair, in my experience of Java programming I haven't wasted much time on writing a few extra tokens and tracking down an error because of lack of type-safety here, but nonetheless, this is nice to have. One benefit of C# is the nice surprise you get when debugging - if you put a break point on an enumeration which holds combined enumerations, it will automatically decode direction to give you a human readable output, rather than a number you have to decode:

Declaration:

public enum Direction {North=1, East=2, West=4, South=8};
Usage:
Direction direction = Direction.North | Direction.West;
if ((direction & Direction.North) != 0)
    ....

If you put a breakpoint on the if statement, you'll get the human readable version for direction rather than the number 5.

The mostly likely reason why enums are absent in Java is that you can get by using classes instead. As I mentioned in previous sections, with classes alone we are not able to express a feature in the world as well as we could with another construct. What are the benefits of the Java philosophy of "if it can be done by a class, then don't introduce a new construct"? It would seem simplicity would be the biggest benefit - a shorter learning curve and the prevention of programmers having to think of multiple ways of doing things. Indeed the Java language has improved on C++ in many ways by aiming for simplicity, such as the elimination of pointers, the elimination of header files, and a single-rooted object hierarchy. However, a common aspect of all these simplifications is they actually make coding - uh - simpler. Leaving out constructs, we've looked at enums, properties and events so far, makes your coding more complicated.

7. Collections and the Foreach Statement

C# provides a shorthand for for-loops, which also encourages increased consistency for collections classes:

In Java or C++:

1. while (! collection.isEmpty()) {
        Object o = collection.get();
        collection.next();
	...
2. for (int i = 0; i < array.length; i++)...

In C#:

1. foreach (object o in collection)...
2. foreach (int i in array)...

The C# for-loop will work on collection objects (arrays implement a collection). Collection objects have a GetEnumerator() method which returns an Enumerator object. An Enumerator object has a MoveNext method and a Current property.


Previous     Next


C# 3.0 in a Nutshell (April 2007) - by Joseph Albahari and Ben Albahari.









Add To Favorites
Email This Page



Side Panel
Privacy Policy About Us Contact Us