Nice Things About C#

I'm really a fan of the high-level aspects of langages like Java; no more allocating/deallocaing memory, objects all over the place, flexibility of deployment. Yet I think a lot of features are missing from Java, and so I wanted to discover if C# made up for a lot of those. Here's a list of nice things about C#, especially compared to Java

Postscript: I wrote this some time ago (Spring 2003). While I still like the features of C#, and think it is a fabulous language, unfortunately the performance of generated code is very slow, and as such it is not currently useful for my work. This has forced me to switch back to using C++. However, I hope that these problems will eventually be remedied.

Operator Overloading
In my opinion, this is the best thing about object-oriented languages, because it allows to to create new types that act like existing types. I do a lot of work with computer graphics, and its nice to be able to have vectors and matrices that support being added, multiplied, etc. without having to call explicit functions.
All variables are objects
C# is truly object oriented. All data types are objects, and can be used in generic data structures. This eliminates the need for shadow classes that duplicated all of the primitive types
Value types (structs)
In C#, classes and structs are different. Structs act like primitives, in that when you make a statement like
a = b
where a and b are structs (also known as value types), you will make a true copy of b. When using a class in C# or Java, the assignment would make a new reference to the object, but both references would point to the same object.
Properties
This is another nice convenience feature that allows a class to have data members that are not variables, but functions that are accessed through a variable like syntax. This allows for nicer encapsulation, and allows for dynamically computed data members. It also allows for a nicer syntax to replace "setter" functions.
Custom Arrays / Iterators
We can use this to make custom arrays that are compatible with the foreach statement. In the upcoming version of C#, they have features that allow very easy creation of iterations though the yield statement, that will return a value, but when the function is called again, will start at the same place. Think recursive traversal of a tree, and at the "visit" portion, you yield a value. With C#, you can then do this traversal in a foreach.
Out params / pass-by-reference
In Java, while all objects are represented as references, parameter passing is always pass-by-value. Therefore, a caller cannot change the value of a named local variable that has been passed to it by its caller, even when such behavior is desired. Also, it is difficult to allocate multiple new objects in a function and return then to the caller, because there can be only one return value. The solution in C# is true pass-by-reference, as well as out parameters that act as extra return values. Both must be specified explicitly by both caller and callee, removing a source of confusing.
More flexible namespaces
C# does not force a directory structure on your program. Namespaces are indicated by the namespace blocks, which can wrap arbitrary regions of a file, rather than a simple package statement at the top of the file that implies everything is in the same package.
Must specifically override or redefine virtuals
In Java, all methods are assumed to be virtual (to use the C++ jargon) and methods with equivalent signatures in child classes will automatically override. In C#, the keyword "override" must be used, or the keyword "new" must be used to redefine a function. This provides a clarity benefit.
No switch case fallthrough
Finally! There is no good reason to have fallthrough in the first place, but someone finally got the courage to get rid of it. Just in case you need to fallthrough, you can use the goto syntax.
Variable-length argument lists - printf style formatted output
It always bothered me that there is no printf analog in Java. Being forced to put extra plus signs and closing strings every time you want to stick another variable in a string is tedious, but required in Java due to a lack of variable-length argument lists. It could be duplicated by creating a generic array on the spot, but because primitives are not objects, this complicates things. C# provides this, but by putting the variable arguments in an array, it provides a much a nicer way then the hacked up varargs of C++
unsafe mode - essentially lets you embed C++ (esp. pointer stuff)
While controversial, C# allows the user to code unsafely if they so choose. Within the context of an unsafe block, C# allows additional operators for C-style pointer referencing, dereferencing and other such operations. If these low-level operations are needed for some reason, perhaps to adapt to a legacy interface, they are there if you need them, without having to resort to complex interface layers like JNI
Attributes
In most languages, the per-class and per-object metadata is very limited (if it exists at all). In Java, you can reflect on the names of classes, but in C#, you can associate arbitrary information with any class or method as an "Attribute". This is already used for their DLL-import system, as well as allowing specifications for remoting. But you could use it for anything, and it uses a simple syntax.
Keeps a lot of the C preprocessor for conditional compilation
You can still do things like #ifdef, when necessary. This can be useful when one branch simply will not compile unless certain conditions are set
Multidimensional arrays
Another thing that was finally put into commercial programming languages: true multidimensional arrays. While Java has a similar syntax to multidimensional arrays, there are really arrays of arrays. This means that you cannot allocated a multi-dimensional array all at once, and you have no guarantee that all of the sub-arrays are of the same length.
foreach
Along with the iterators, we can explicitly iterate on each element of some collection. While this is syntactic sugar more than anything else, it is a nice feature to simplify collections, especially non-indexible ones.
enumerations
When you want a simple list of constants, without concern for what their values are, you want an enum. C++ had them, Java got rid of them, C# puts them back in.
delegates (object oriented, checked function pointers)
This feature, added into Microsoft's implementation of Java, was the main reason that Sun sued Microsoft for copyright infringement. This controversial features is essentially a typesafe function pointer that can be used to call back into a particular class.

Copyright © 2005 Christopher DeCoro