Quote:
Originally Posted by .John
I understand it exists in D... so back to the original question - why D if C# provides all this, and more. Like I said... the only valid reason I see is you just wanted to experiment with it.
Also, while I'm not sure, nor do I have data - I think C# actually compiles just as fast, if not faster than D, and generic programming patterns in C# are far better than D. Also D looks pretty ugly from a syntax / design point of view.
|
That's your opinion. Also what does C# provide that D doesn't? Generic programming paterns in C# are not better than D, since D can access variables and any types of functions pretty much at compile time, then D templates beats C# generics and even C++ templates. You can write in the exact same syntax as C# if you want to, however there is a standard guideline of course which most people follow. I can't see how D has an ugly syntax when it pretty much follows a traditional C syntax except for the way templates are accessed using !template_params rather than <template_params>. D has conditional programming, contracts and unittesting implemented in the language. C# has neither of them, except for maybe some preprocessor, but that again
sucks IMO.
Let's do some comparisons with D and C#.
Appending to an array in C#
Code:
Array.Resize(a, a.Length + 1);
a[a.Length - 1] = value;
Appending to an array in D
Creating a hash map in C#.
Code:
Dictionary<TKey,TValue> dict = new Dictionary<TKey,TVvalue>();
Creating a hash map in D.
Variadic parameters in C#.
Code:
MyFunction(int param1, int param2, params T[] variadicParams)
Variadic parameters in D.
Code:
myFunction(int param1, int param2, ...)
The usage seem to be better in C# as it handles the actual parameter retrieving where you have to do it yourself using va_start and va_args in D.
Properties in C#.
Code:
int myInt;
int MyInt
{
get { return myInt; }
set
{
if (value > 100)
value = 100;
myInt = value;
}
}
Properties in D.
Code:
int _myInt;
@property {
int myInt() { return m_myInt; }
void myInt(int value) {
m_myInt = value;
}
}
The layout is better in C#, but the abstraction is better in D as you can choose the get and set types yourself. In C# the get and set type has to be equal.
Constructors in C#.
Code:
public class MyBaseClass
{
int MyBaseValue;
public MyBaseClass(int value)
{
MyBaseValue = value;
}
}
public class MyClass : MyBaseClass
{
public MyBaseClass(int value)
: base(value)
{
}
}
Constructors in D.
Code:
class MyBaseClass {
int myBaseValue;
this(int value) {
myBaseValue = value;
}
}
class MyClass : MyBaseClass {
this(int value) {
super(value);
}
}
D constructors are superior in the sense that you can call base (super) constructors anywhere within your constructor body and you can call other constructors too wherever you want.
This makes sense if you want to do some stuff before initializing the base class.
Changing the string length in C#.
Code:
// Way 1
var sb = new StringBuilder(myString);
sb.Length = newLength;
myString = sb.ToString();
// Way 2
myString = myString.SubString(0, newLength);
Changing the string length in D.
Code:
myString.length = newLength;
D strings are superior to C# strings, because they're treated exactly as arrays and not as an object type. Which means resizing an array in D is the same way, unlike C# where you have to call a Array.Resize() etc.
also C# requires a lot of copying to do such things, which isn't necessary in D.
Slicing an array in C#
Code:
// Way 1
var sliced = new T[sliceSize];
Array.Copy(myArray, sliceStart, sliced, 0, sliceLength);
// Way 2
var sliced = myArray.Take(sliceLength); // Forced at offset 0 I think so...
// Way 3
var segment = new ArraySegment<T>(myArray, sliceStart, sliceLength);
Slicing an array in D
Code:
auto sliced = myArray[sliceStart .. sliceEnd];
Note: The $ symbol equals the length of the identifier ex.
Code:
auto sliced = myArray[0 .. $];
Creates a slice of the whole array.
D slices are superior to C# slices because it doesn't create a new array, it points directly to the original data. So if you modify the slice you also modify the original array.
If you want to copy you can use .dup which duplicates the array to a mutable array or .idup which duplicates the array to an immutable (read only) array.
Ref/Out parameters in C#
Code:
// Declaration
MyFunction(ref myRef)
// Call
MyFunction(ref passedVar);
Ref/Out parameters in D
Code:
// Declaration
myFunction(ref myRef)
// Call
myFunction(passedVar);
String enums in C#
Code:
N/A unless you do some "hacking" using attributes, but the enumeration value still need a primitive type.
String enums in D
Code:
enum MyEnum {
value1 = "Hello",
value2 = "World!"
}
C# LINQ VS D
Code:
// C# | D
// Set
Distinct | uniq
Except | setDifference
Intersect | setIntersection
Union | setUnion
// Aggregration
Aggregrate | reduce
Average | N/A
Count | count
Min | min
Max | max
// Sorting
OrderBy | sort
OrderByDescending | sort
ThenBy | N/A
ThenByDescending | N/A
Reverse | reverse
// Other
Where | filter
GroupBy | group
Contains | canFind
Repeat | fill
SequenceEqual | equal
Concat | join
LINQ seems to be more complete in C#, but it has also existed far longer. D is a relative new language, but has expanded so much in the past few years.
IMO I chose D because I can produce codes faster in D and it's more obvious what my code does. The whole .NET library is so ugh, it does so many illogical operations behind the scene and IMO I'd like to know what's going on.
An example is Array.Resize() the logical explanation for it is that it resizes an array. However it doesn't really resize. It just makes a new array with a new size and copies the data from the old array into the new array.
Also I like the fact that I don't have to encapsulate everything and only when needed. There is a lot other things that made me choose D over C#.
Notice: I left out some features in D that C# doesn't have as it had to be a comparison like C# can't do inline asm, contracts as build in, conditional compliation (static if, version etc.) I tried to compare common things with C# and D that they do differently.
IMO the only things I think are better in C# than D is properties and variadic parameters.
#Edit got another thing to compare
Looping a hash map in C#
Code:
foreach (var keyValue in dict)
{
keyValue.Key // Key
keyValue.Value // Value
}
Looping a hash map in D
Code:
foreach (key, value; dict) {
// ...
}
Looping by range in C#
Code:
for (int i = 0; i < 10; i++)
{
// ...
}
Looping by range in D (Of course there is for loops too.
Code:
foreach (i; 0 .. 10) {
// ...
}
Parallele foreach in C#
Code:
Parallel.ForEach(a, elem =>
{
// ... use elem
});
Parallele foreach in D
Code:
foreach (i, ref elem; taskPool.parallel(a)) {
// ... use elem OR i
}
Okay I think that's it. I'm not saying D does these things better, this is merely a comparison, but IMO I like the way D does the things better.
As a last thing I think you should read this