Difference between revisions of "Programming in D for CSharp Programmers"
m (O3o moved page Coming From/C Sharp to Programming in D for CSharp Programmers) |
|||
Line 1: | Line 1: | ||
+ | = Introduction: Hello workd= | ||
+ | |||
+ | ''The C# Way'' | ||
+ | |||
+ | using System; | ||
+ | class Hello | ||
+ | { | ||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | Console.WriteLine("Hello world!"); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ''The D Way'' | ||
+ | |||
+ | import std.stdio; | ||
+ | void main(string[] args) | ||
+ | { | ||
+ | writeln("Hello world"); | ||
+ | } | ||
+ | |||
+ | Things we learnt so far: | ||
+ | |||
+ | * standard file extension for D source code files is <code>.d</code></li> | ||
+ | * we <code>import</code> a module instead of <code>using</code> a namespace;</li> | ||
+ | * static methods can be declared outside a class;</li> | ||
+ | * we can call directly any method even if it’s not declared in a class (<code>writeln</code>);</li> | ||
+ | * <code>writeln</code> is D equivalent for C# <code>Console.WriteLine</code>;</li> | ||
+ | * syntax is exactly the same as in C# (method definitions, string qualifiers, array declarations, comments) | ||
+ | * many of the keywords are exactly the same (<code>void</code>, <code>string</code>); | ||
+ | |||
+ | = Coding styles = | ||
+ | |||
+ | * D programmers prefer to use the camelCase notation instead of PascalCase for method names, variable names and enum members; | ||
+ | * Module names (C# namespaces) are always in lowercase due to cross-platform compatibility regarding file names. | ||
+ | * If there are conflicts between a named entity and a keyword, in C# you can use verbatim identifiers (@while). D does not have verbatim identifiers, but the convention is to add an underscore at the end of the entity (while_). | ||
+ | |||
+ | = Type system = | ||
+ | |||
+ | == Built-in types == | ||
+ | |||
+ | Basic type names are very similar in both languages with the following differences: | ||
+ | * The 8 bit signed integer from C# <code>sbyte</code> is written in D as <code>byte</code>; | ||
+ | * The 8 bit unsigned integer from C# <code>byte</code> is written in D as <code>ubyte</code>; | ||
+ | * There is no type equivalence for <code>decimal</code> | ||
+ | * There are three types of char in D: <code>char</code>, <code>wchar</code> and <code>dchar</code>, each of them corresponding to an UTF encoding : UTF-8, UTF-16 and UTF-32. Since C# is using internally UTF-16 chars, the direct equivalent of C# <code>char</code> is D <code>wchar</code>. | ||
+ | * There are also three types of string in D: <code>string</code>, <code>wstring</code> and <code>dstring</code>. The direct equivalent of C# <code>string</code> is in fact D <code>wstring</code>. These are not keywords in D, they are in fact declared as aliases to immutable char arrays. | ||
+ | * There is another floating point type in D: <code>real</code> with no type equivalence in C#. | ||
+ | * Complex floating point types <code>Complex<T></code> are keywords in D: <code>cfloat, cdouble, creal</code> and they imaginary counterparts are <code>ifloat</code>, <code>idouble</code>, <code>ireal</code>; | ||
+ | |||
+ | == Arrays == | ||
+ | |||
+ | Arrays in D are not too different than the ones form C# | ||
+ | |||
+ | ''The C# Way'' | ||
+ | <pre> | ||
+ | int[] array; | ||
+ | fixed int array[20]; | ||
+ | </pre> | ||
+ | |||
+ | ''The D Way'' | ||
+ | <pre>int[] array; | ||
+ | int array[20] | ||
+ | </pre> | ||
+ | |||
+ | == Pointers == | ||
+ | Since D is not a managed language, you are free to use pointers anywhere in the code, without encompassing them in an unsafe context. On the contrary, D code is by default unsafe, but you can force the safe context using the <code>@safe</code> keyword:</li> | ||
+ | |||
+ | ''The C# Way'' | ||
+ | <pre>int value; | ||
+ | //here you can't use pointers | ||
+ | unsafe { | ||
+ | int * p = &value | ||
+ | }</pre> | ||
+ | |||
+ | ''The D Way'' | ||
+ | |||
+ | <pre>int value; | ||
+ | int * p = &value | ||
+ | @safe { | ||
+ | //here you can't use pointers | ||
+ | }</pre> | ||
+ | |||
+ | == Delegates == | ||
+ | |||
+ | Delegates in D are declared with the same keyword, but the return type precedes the declaration: | ||
+ | |||
+ | ''The C# Way'' | ||
+ | delegate int F | ||
+ | |||
+ | ''The D Way'' | ||
+ | int delegate(int x) Foo; | ||
+ | int function(int x) Foo; | ||
+ | |||
+ | Since D doesn't need to declare methods inside a class, you can declare also a function, equivalent to a delegate without class context. A notable difference between C# and D is the fact that '''delegates are not multicast in D''', therefore you cannot join or remove them. | ||
+ | |||
+ | == Enums == | ||
+ | |||
+ | There is no difference between enum declarations, except that so called C# flags enums are not necessarely decorated with the [Flags] attribute: | ||
+ | |||
+ | ''The C# Way'' | ||
+ | enum Option { | ||
+ | Option1, | ||
+ | Option2 | ||
+ | } | ||
+ | [Flags] | ||
+ | enum Options { | ||
+ | Option1, | ||
+ | Option2, | ||
+ | All = Option1 | Option2 | ||
+ | } | ||
+ | |||
+ | ''The D Way'' | ||
+ | |||
+ | enum Option { | ||
+ | Option1, | ||
+ | Option2 | ||
+ | } | ||
+ | |||
+ | enum Options { | ||
+ | Option1, | ||
+ | Option2, | ||
+ | All = Option1 | Option2 | ||
+ | } | ||
+ | |||
+ | |||
[http://www.youtube.com/watch?v=6_xdfSVRrKo C# to D video-presentation] | [http://www.youtube.com/watch?v=6_xdfSVRrKo C# to D video-presentation] | ||
[[Category:Languages versus D]] | [[Category:Languages versus D]] |
Revision as of 16:24, 15 April 2015
Contents
Introduction: Hello workd
The C# Way
using System; class Hello { static void Main(string[] args) { Console.WriteLine("Hello world!"); } }
The D Way
import std.stdio; void main(string[] args) { writeln("Hello world"); }
Things we learnt so far:
- standard file extension for D source code files is
.d
- we
import
a module instead ofusing
a namespace; - static methods can be declared outside a class;
- we can call directly any method even if it’s not declared in a class (
writeln
); writeln
is D equivalent for C#Console.WriteLine
;- syntax is exactly the same as in C# (method definitions, string qualifiers, array declarations, comments)
- many of the keywords are exactly the same (
void
,string
);
Coding styles
- D programmers prefer to use the camelCase notation instead of PascalCase for method names, variable names and enum members;
- Module names (C# namespaces) are always in lowercase due to cross-platform compatibility regarding file names.
- If there are conflicts between a named entity and a keyword, in C# you can use verbatim identifiers (@while). D does not have verbatim identifiers, but the convention is to add an underscore at the end of the entity (while_).
Type system
Built-in types
Basic type names are very similar in both languages with the following differences:
- The 8 bit signed integer from C#
sbyte
is written in D asbyte
; - The 8 bit unsigned integer from C#
byte
is written in D asubyte
; - There is no type equivalence for
decimal
- There are three types of char in D:
char
,wchar
anddchar
, each of them corresponding to an UTF encoding : UTF-8, UTF-16 and UTF-32. Since C# is using internally UTF-16 chars, the direct equivalent of C#char
is Dwchar
. - There are also three types of string in D:
string
,wstring
anddstring
. The direct equivalent of C#string
is in fact Dwstring
. These are not keywords in D, they are in fact declared as aliases to immutable char arrays. - There is another floating point type in D:
real
with no type equivalence in C#. - Complex floating point types
Complex<T>
are keywords in D:cfloat, cdouble, creal
and they imaginary counterparts areifloat
,idouble
,ireal
;
Arrays
Arrays in D are not too different than the ones form C#
The C# Way
int[] array; fixed int array[20];
The D Way
int[] array; int array[20]
Pointers
Since D is not a managed language, you are free to use pointers anywhere in the code, without encompassing them in an unsafe context. On the contrary, D code is by default unsafe, but you can force the safe context using the @safe
keyword:
The C# Way
int value; //here you can't use pointers unsafe { int * p = &value }
The D Way
int value; int * p = &value @safe { //here you can't use pointers }
Delegates
Delegates in D are declared with the same keyword, but the return type precedes the declaration:
The C# Way
delegate int F
The D Way
int delegate(int x) Foo; int function(int x) Foo;
Since D doesn't need to declare methods inside a class, you can declare also a function, equivalent to a delegate without class context. A notable difference between C# and D is the fact that delegates are not multicast in D, therefore you cannot join or remove them.
Enums
There is no difference between enum declarations, except that so called C# flags enums are not necessarely decorated with the [Flags] attribute:
The C# Way
enum Option { Option1, Option2 } [Flags] enum Options { Option1, Option2, All = Option1 | Option2 }
The D Way
enum Option { Option1, Option2 }
enum Options { Option1, Option2, All = Option1 | Option2 }