Wednesday, January 27, 2016

Few notes on C# and Pascal

My few notes while migrating from Pascal to C#.


Language syntax is quite simple and won't take much time to learn. Though you need some time to learn framework/classes. It's not big problem, there are many examples online. Latest versions of Delphi move to C# way of things, like Generic collections, StringBuilder, System.Net.

Language is case-sensitive.

No include files / preprocessing. There are defines, similar to Pascal.

All code belongs to classes, classes belong to namespaces. All this is located in assemblies (dll or exe).

C# project in Visual Studio can easily reference other assemblies with few clicks.

Roslyn compiler is integrated into latest Visual Studio, and checks the project in background, so you get warnings/error early.

Many built-in classes. Though for me sometime it's not enough :) For example, I wonder why there's no built-in Red-Black Tree class (though Dictionary and Set classes use RBTree internally). Anyway  a lot of code you can find implemented open-source.

Even alternative IDE SharpDevelop is open-source, with its own SynEdit analogue called AvalonEdit.

Managed code. Sources compiled to intermediate language and are easy to reverse-engineer.

Code can be slow if it is executed purely by IL interpreter, but in practice just-in-time compiler translates functions to native code which runs with good speed.

IL code can be easily decompiled with ILSpy or Reflector, of course, if it is not obfuscated.

Garbage collection makes code cleaner, without such pattern:
my = TMyClass.Create()
 You just do:
var my = new TMyClass();
Have to pay attention to things that wrap system resources, like handles. For example file stream, it implements IDisposable interface. You can use try-catch-finally or using keyword specially for IDisposable:

using (var fs = new FileStream(...))

You can declare variable in-place. Also var means it will detect type from the right side of assignment. You can specify type manually.

You can initialize fields this way
var my = new MyClass
field1 = 1,
field2 = 2,
field3 = 3,

Switch structure can use string literal as cases. This is already implemented in FreePascal (translated as series of if-else). C# implementation can use if-else branches too (when there are few cases) or it will create dictionary behind the scenes.

There are still no binary literals, but that seems to be fixed soon.

Iterators can be made simple by using yield keyword. Simple example:
yield return 1;
yield return 2;
yield return 3;
Compiler will take care of internals and creates IEnumerable to walk 1, 2, 3.

Object casting with AS and checking with IS are similar to Pascal.

You can easily format string, putting arguments right in string, like
string str = $"The amount is {theAmount}";
Simple functions or properties don't even need explicit body, like this (function to return int):
int SimpleFunc() => 123; 
These are just few notes.

Using C# and Visual Studio is unusual first, but in general, developing becomes easier and more stable.

No comments:

Post a Comment