C# Language Notes
Exploring C# from a background of C++ and Java, I found that most C# language features are fairly intuitive. Here's a list of issues that I found to be exceptions.
Confusing Distinctions
- readonly vs. const: The difference between readonly and const is not explained clearly in the VS.NET documentation. It makes more sense once you realize that const is inlined at compile-time while readonly is evaluated at run-time. See the excellent Comparative Overview of C#.
- event vs. delegate: Another distinction is not made clear by the documentation, this time between an event and a delegate-typed object. The article C# events vs. delegates does a great job of clarifying the differences.
- Destructors vs. Finalize(): I'm still not sure of the exact difference between a destructor and the Finalize() method. One functional distinction is that the destructor automatically invokes the base's destructor. In light of that, it makes sense that a Finalize() method can be found on the Object class since the Object class has no base class. But are we supposed to write destructors or a Finalize() in our classes?
It seems that the recommendation for user classes is to write destructors. But I notice that the Finalize() method is mentioned in classes other than the Object class. So which is it?
C# vs. Java Quick Reference
Here are some comparisons between the two similar languages that are not mentioned by the article
Comparative Overview of C#.
- Types:In Java, to get a Class object, you use either Class.forName("ClassNameString") or object.GetClass(). You can then call getName() or toString() on the Class object to get the name in string form. In C#, to get a Type object, you use either typeof(ClassNameString) or object.GetType(), which unlike Java work on primitive types as well. You can then reference properties Name or FullName or call ToString() on the Type object to get the name in string form.
- As:From the documentation:
The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form:
expression as type
is equivalent to:
expression is type ? (type)expression : (type)null
except that expression is evaluated only once.
Posted by juliob at October 04, 2003 09:36 PM