New C# Language
Features
I've been considering whether to update my
C# vs. Java article with the upcoming features
of both languages which seem to be converging ever
faster given the announcements Sun has made about
Tiger 1.5. It seems the conclusion I drew in
the article thatIt is my opinion that
both languages are similar enough that they could
be made to mirror each other without significant
effort if so required by either user base. In
this case, C# would have it easier than Java in
that C# has less to borrow from Java than Java
would have to borrow from C#.
is turning out to be very true. The article gets
about 1500 to 2000 hits a month so I assume there
are literally 1000s of developers who would benefit
if I did so. I wonder what any of the readers of
the original article think?
I talked to
Eric Gunnerson a few days before OOPSLA about
his teams plans for C# but was still blown away by
Anders Hejlsberg's presentation. The most
compelling feature to me is generics but the devs
on my team seem to like iterators a lot.
Specifically I think
- Generics: This feature
really feels like C++ Templates++. Besides
compile time checking and being able to apply on
everything from classes to methods they do one
thing that C++ templates do not but Bjarne
Stroustrup has been talking about for a while. C#
generics support constraints on generic arguments
(read the topic
Constraints for template arguments in my "C++ in
2005" article for more info on this from the
C++ perspective) using a
where
clause. The following code snippet from the
presentation is shows this supremely cool
feature
interface IComparable<T> { int
CompareTo(T obj); }
class Dictionary<K, V>: IDictionary<K,
V> where
K: IComparable<K>,
V: IKeyProvider<K>,
V: IPersistable,
V: new()
{
...
}
- Iterators: C# enables any
class to be iterated over using the foreach
keyword
as long as it contains a method named
GetEnumerator() that returns a type with a
Current property and a MoveNext() method.
The compiler knows to convert each
foreach
to the calls to the
appropriate methods. Iterators seem to add more
indirection via foreach()
member
functions which now allow the compiler not only
to rewrite the foreach
loop
correctly but also codegen the GetEnumerator()
method and the type it returns. A practical
example from the presentation is that the
code
public class List
{
internal object[] elements;
internal int count;
public ListEnumerator
GetEnumerator() { return
new ListEnumerator(this);
}
}
public class ListEnumerator
{
List list;
int index;
object current;
internal ListEnumerator(List list)
{
this.list = list;
}
public bool MoveNext() {
if (index >=
list.count) {
current =
null;
return
false;
}
current =
list.elements[index++];
return true;
}
public object Current {
get { return current;
}
}
}
shrinks to
public class List
{
internal object[] elements;
internal int count;
public object foreach() {
for (int i = 0; i <
count; i++) yield elements[i];
}
}
OK, that yield
keyword is pretty
cool.
- Anonymous Methods: This
fixes the lack of anonymous inner classes in C#
by adding anonymous methods for use in the exact
same situations most people use anonymous inner
classes for. I actually think it is cleaner for
it to be anonymous methods and that this brings
C# one step closer to having
closures.
- Partial Types: This is a
pretty ho hum feature to me. Allowing classes to
be defined in multiple files seems to be bowing
to user pressure from users who have bad design.
If your class is so big that it needs to be
defined in multiple files then doesn't that
create a
bad smell?
I have raised an issue with the C# folks about what
adding new keywords to the language would mean for
people who try to compile apps written against v1.0
of C# and used variable names like
yield or
partial. They seem to be aware of it but
feedback from the C# user community at large will
probably have more impact in deciding how they'll
tackle this problem.
#I-Spy Movie
Review
This is yet another mismatched-pair buddy
cop spy movie. Good movies in this
genre include the
Rush
Hour and
Lethal Weapon
series while bad movies in this genre include
garbage like
Showtime.
I-Spy is no Rush Hour but it did have a few scenes
which I considered laugh out loud funny. The
funniest scene is a redo of the formulaic
Cyrano
De Bergerac joke where the smooth playa (Eddie
Murphy) puts words in the mouth of the novice at
love (Wilson) in attempt to help him get the girl.
The plot isn't much (invisible jet? riiiiight) but
is just enough to hang a number of formulaic jokes
off of. The only reason I wouldn't give this movie
two thumbs up is that they resorted to the most
overused spy movie clichè in a move that
actually detracted from the plot instead of adding
to it.
Carnage4Life Score: *** out of *****
#Seattle's Wonderful
Weather
10-Day Forecast for Seattle, WA. My girlfriend
owes me a viewing of the
Lord of the Rings: Fellowship of the Ring DVD
the next rainy weekend we have together. It looks
like next weekend is it. Yaaay, Seattle
weather.
#