D vs. C#
Julio César Carrascal Urquijo
jcesar at phreaker.net
Thu Nov 23 20:34:21 PST 2006
Frank Benoit (keinfarbton) wrote:
> I want to write an article about D. Therefore I want to show more
> details and advantages of D compared to C++, Java and C#. But I never
> used C#.
>
> What are concrete things that are better in D or C#? Can you make examples?
I'm just going to compare languages because a comparison between .NET's
Base Class Library and Phobos isn't fair anyway. This isn't an
exhaustive list, is just what I remembered at the moment.
D vs C# 2.0
Generics / templates are a lot more powerful in D. In C#, for a generic
parameter you can only call methods defined on Object unless you tell
the compiler that it will be of a predefined (base) class witch pretty
much defeats the purpose of a generic class in most cases.
This limitation makes generics useful mostly for container types and
impedes advanced meta-programming like what's available on D.
Properties in C# are declared explicitly using special syntax. D looks
for methods with certain signatures to implement properties. Some people
prefer one style over the other but the result is pretty much the same.
C# has partial classes witch allows split a class in several files. This
can be handy, for example, for a code generator to safely modify a file
while the user modifies another for the same class. D doesn't has any
way of doing this.
Events are a native construct on C#. D recently got a standard library
module that implements Signals & Slots witch is a similar concept but
it's not in the language like in C#.
C# has anonymous delegates as does D but they managed to implement them
as real closures so you can return them from a function. I use these a
lot, they cut down work every where. In D returning a delegate or using
it after the function ended is undefined and probably will get you an
Access Violation.
D has lazy parameter evaluation where one or more parameters of a
function aren't evaluated until they are needed. Very useful feature and
one coming straight from functional languages. C# doesn't have this feature.
C# things that don't have an equivalent in D.
* Syntax for nullable types.
* Operator ?? for null testing.
* yield operator
D things that don't have an equivalent in C#.
* Tupples.
* Unions.
* Mixins.
* DBC.
* Inline Assembler.
D vs C# 3.0
Release date for C# 3.0 hasn't been announced yet. Walter announced a
1.0 version for January 1 of 2007.
Extension methods in C# will allow you to add new methods for any type.
So, if you want the String class to have a method that converts to camel
convention you can add it and call it like s1.ToCamelCase(). D supports
something like extension methods but only for arrays of any type, just
declare a function with a parameter of the type you want to extend.
Lambda expression are supported in both languages although the C# syntax
is a little better.
In C# 3.0 we will have Query Expressions. These are a kind of run-time
syntax tree of any code block you declare as a query expression and pass
it around to methods, serialize it or compile and execute it. If you've
programmed in Lisp, you'll recognize this. In D... Not yet but we are
getting there with Tupples. Je je.
Language integrated query (LINQ) witch more or less is SQL integrated in
the language. In C# 3.0 it is implemented as syntax-tree transformations
to the features described before. Cool stuff. Nothing like this in D has
been discussed.
As far as I know, generics weren't improved much on C# 3.0 so D still
beats C#s a** on this.
Hope this helps.
--
Julio César Carrascal Urquijo
More information about the Digitalmars-d
mailing list