Recent

Author Topic: What are main differences between free pascal and c#?  (Read 3613 times)

DecayAssault

  • New Member
  • *
  • Posts: 13
What are main differences between free pascal and c#?
« on: January 30, 2023, 10:23:51 pm »
Hi!
I have started from c# (WinForms, asp.net). Could I use Lazarus instead of them? Thanks a lot.

VisualLab

  • Sr. Member
  • ****
  • Posts: 291
Re: What are main differences between free pascal and c#?
« Reply #1 on: January 30, 2023, 11:08:22 pm »
C# is a language. Lazarus is a IDE (RAD) like Visual Studio or Delphi. The programming language used in Lazarus is Object Pascal (Pascal too). And compiler is Free Pascal.

C#:
  • source code compiled to intermediate code (EXE or DLL are containers for byte-code),
  • fully object-oriented - all code must be in classes (like in Java),
  • poor support in systems other than Windows (but there is),
  • language and tools entirely dependent on the "whim" of the corporate management (Microsoft), which in the absence of competition can be risks for /developers (for now there are Java and C ++)

Object Pascal:
  • compiled to machine code (EXE, DLL),
  • hybrid - source code does not need to be placed in classes,
  • available on several platforms (Windows, Linux, Mac OS, etc.),
  • the language and tools have little influence from their users/developers (small but better than none).

DecayAssault

  • New Member
  • *
  • Posts: 13
Re: What are main differences between free pascal and c#?
« Reply #2 on: January 31, 2023, 12:26:34 am »
Thanks a lot.
Is there a way I can use to iterate strings like arrays?

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: What are main differences between free pascal and c#?
« Reply #3 on: January 31, 2023, 12:53:04 am »

C#:
  • source code compiled to intermediate code (EXE or DLL are containers for byte-code),
  • fully object-oriented - all code must be in classes (like in Java),
  • poor support in systems other than Windows (but there is),
  • language and tools entirely dependent on the "whim" of the corporate management (Microsoft), which in the absence of competition can be risks for /developers (for now there are Java and C ++)
Thats a  bit outdated, First .net core, the cross platform alternative is widely used and supported, so while it provides a different set of base functionality compared to .net framework, it is quite stable and we'll established and allows for easy cross platform development

Secondly the language was made open source (the specification and also the tooling for .net core) and new features and developments are decided in direct cooperation with the community (any feature request that has a certain number of upvotes will be considered and at least be answered).
So it's not up to the whim of Microsoft, but actually pretty democratic.

That said if you need .Net for windows, this is still completely under Microsoft's control, and .net core and .net framework are quite different, so you need to decide early on what you want or need for a certain project


Aside from that, on a language level, the two languages are very different, so different that it does not make sense to list all the differences here.
The most important one is probably that C# is managed while pascal requires manual memory management

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: What are main differences between free pascal and c#?
« Reply #4 on: January 31, 2023, 01:06:16 am »
Thanks a lot.
Is there a way I can use to iterate strings like arrays?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. // for windows that should be used to have console opened without need to check project options
  3. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  4.  
  5. // initialize two different kind of things for demo
  6. const
  7.   CString = string('Hello World!');
  8.   CArray : array[0..1] of string = ('Hello', 'World!');
  9. // define variables that we need for demo
  10. var
  11.   c: Char;
  12.   i: Integer;
  13. begin
  14.   // demo to iterate over a string
  15.   for c in CString do
  16.     Write(c); // display each char
  17.   // linebreak
  18.   WriteLn;
  19.   // demo to iterate over a array of strings
  20.   for i := Low(CArray) to High(CArray) do
  21.     Write(CArray[i] + ' '); // display each string (and add a space between)
  22.   // linebreak
  23.   WriteLn;
  24.   // for windows wait that user press return key
  25.   {$IFDEF MSWINDOWS}ReadLn;{$ENDIF}
  26. end.

Does that help?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

DecayAssault

  • New Member
  • *
  • Posts: 13
Re: What are main differences between free pascal and c#?
« Reply #5 on: January 31, 2023, 10:31:15 am »
In what tasks you would prefer Object Pascal?
I mean that a string literal "Hello" is an array of characters 'H', 'e', 'l', 'l', 'o'.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: What are main differences between free pascal and c#?
« Reply #6 on: January 31, 2023, 11:26:18 am »
In what tasks you would prefer Object Pascal?
I mean that a string literal "Hello" is an array of characters 'H', 'e', 'l', 'l', 'o'.

In general I choose what I use based on what I want to do. So basically I would only use C# if a customer forces me too, or if I want ASP.NET.

Even when I was fulltime ASP.NET developer in C#, I used Delphi (lazarus was not that well then) for all GUI work, adminstrative apps, data pumps etc.

It never is just language, but always the total picture of language+tools(platform support!)+ library/framework, what is available and practical, and what dependencies that pulls in.
« Last Edit: January 31, 2023, 12:03:25 pm by marcov »

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: What are main differences between free pascal and c#?
« Reply #7 on: January 31, 2023, 11:53:08 am »
I mean that a string literal "Hello" is an array of characters 'H', 'e', 'l', 'l', 'o'.
It is by default with the exception that the first index represents the length  :)

You could perhaps use a stringhelper to find what you seek: https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.html but it would imho be better to forget about "strings" as being an "array of chars" in the classical c-way (you would then be better off using c then).


KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: What are main differences between free pascal and c#?
« Reply #8 on: January 31, 2023, 11:54:13 am »
In what tasks you would prefer Object Pascal?
My personal main purpose = everything that has nothing to do with creating a WebSite.

I mean that a string literal "Hello" is an array of characters 'H', 'e', 'l', 'l', 'o'.
Is there a way I can use to iterate strings like arrays?
Aha... you was asking and I made for you an easy understandable example that illustrated your question.
Next time I will just answer with a "Yes" or "No".
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

DecayAssault

  • New Member
  • *
  • Posts: 13
Re: What are main differences between free pascal and c#?
« Reply #9 on: January 31, 2023, 12:07:46 pm »
Thank you, guys.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: What are main differences between free pascal and c#?
« Reply #10 on: January 31, 2023, 01:01:19 pm »
Thanks a lot.
Is there a way I can use to iterate strings like arrays?
Aircode
Code: Pascal  [Select][+][-]
  1. Program TestString;
  2. Uses SysUtils;
  3. Var
  4.   i:Integer;
  5.   s:String;
  6. Begin
  7.   s:='Hello World!';
  8.   For i=1 To Length(s) Do Writeln(s[i]); //Indexed access to Characters of String s
  9. End;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: What are main differences between free pascal and c#?
« Reply #11 on: January 31, 2023, 02:19:55 pm »
20 years ago this code for iterating a string would work perfectly fine, but not anymore:
Code: Pascal  [Select][+][-]
  1. var
  2.   s: String;
  3.   i: Integer;
  4. begin
  5.   s := '€';
  6.   for i := 1 to s.Length do
  7.     WriteLn(s[i]);
  8. end.
Result:
Code: Pascal  [Select][+][-]
  1. Ô
  2. é
  3. ¼
  4.  
The reason for this is that nowerdays Lazarus uses UTF8 Strings, where each codepoint in the Unicode space is represented by up to 4 (theoretically up to 6) bytes. ANd in this example the euro symbol is encoded as 3 bytes

There are UTF8 functions available in the LCL, and some also in the RTL, but frankly they are acutally quite hard to use, so a few days ago, when writing my gold engine, i've wrote my own:
Code: Pascal  [Select][+][-]
  1.   TUTF8Iterator = record
  2.     Data: String;
  3.     ScanHead: SizeInt;
  4.  
  5.     function GetEnumerator: TUTF8Iterator; inline;
  6.     function GetCurrent: String; inline;
  7.     function MoveNext: Boolean; inline;
  8.     function MovePrevious: Boolean; inline;
  9.     function EOS: Boolean; inline;
  10.     procedure Backtrack(ToIndex: SizeInt);
  11.     function CopyFrom(StartPos: SizeInt): String; inline;
  12.     property Current: String read GetCurrent;
  13.   end;
  14.  
  15. ...
  16.  
  17. function IterateUTF8(const AString: String; StartIndex: SizeInt = 0): TUTF8Iterator;
  18. begin
  19.   Result.Data := AString;
  20.   Result.ScanHead := StartIndex;
  21. end;
  22.  
  23. function UTF8CharLen(FirstChar: Char): SizeInt;
  24. begin
  25.   Result := 1 + ord((ord(FirstChar) And %11000000) = %11000000)
  26.               + ord((ord(FirstChar) And %11100000) = %11100000)
  27.               + ord((ord(FirstChar) And %11110000) = %11110000);
  28. end;
  29.  
  30. function InUTF8Sequence(CheckChar: Char): Boolean;
  31. begin
  32.   Result := (ord(CheckChar) And %11000000) = %10000000;
  33. end;
  34.  
  35. ...
  36.  
  37. begin
  38.   Result := Self;
  39. end;
  40.  
  41. function TUTF8Iterator.GetCurrent: String;
  42. var
  43.   CharLen: SizeInt;
  44. begin
  45.   CharLen := UTF8CharLen(Data[ScanHead]);
  46.   Result := Copy(Data, ScanHead, CharLen);
  47. end;
  48.  
  49. function TUTF8Iterator.MoveNext: Boolean;
  50. begin
  51.   if ScanHead < 1 then
  52.     Inc(ScanHead)
  53.   else if ScanHead <= Data.Length then
  54.     Inc(ScanHead, UTF8CharLen(Data[ScanHead]));
  55.   Result := ScanHead <= Data.Length;
  56. end;
  57.  
  58. function TUTF8Iterator.MovePrevious: Boolean;
  59. begin
  60.   if ScanHead > Data.Length then
  61.     ScanHead := Data.Length;
  62.   if ScanHead <= 0 then
  63.     Exit(False);
  64.   Dec(ScanHead);
  65.   while (ScanHead > 0) And InUTF8Sequence(Data[ScanHead]) do
  66.     Dec(ScanHead);
  67.   Result := ScanHead > 0;
  68. end;
  69.  
  70. function TUTF8Iterator.EOS: Boolean;
  71. begin
  72.   Result := (ScanHead > Data.Length) Or (Data.Length = 0);
  73. end;
  74.  
  75. procedure TUTF8Iterator.Backtrack(ToIndex: SizeInt);
  76. begin
  77.   ScanHead := ToIndex;
  78.   if InUTF8Sequence(Data[ScanHead]) then
  79.     MovePrevious;
  80. end;
  81.  
  82. function TUTF8Iterator.CopyFrom(StartPos: SizeInt): String;
  83. begin
  84.   Result := Copy(Data, StartPos, ScanHead - StartPos + 1);
  85. end;

Which then allows iteration like this:
Code: Pascal  [Select][+][-]
  1.   s := '€$£';
  2.   for utf8char in IterateUTF8(s) do
  3.     WriteLn(utf8char);

Alternatively with the LCL, you can use the LazUTF8 unit which provides you this very intuitive and easy to understand functionality:
Code: Pascal  [Select][+][-]
  1. uses LazUTF8
  2. ...
  3. var
  4.   CurP, EndP: PChar;
  5.   Len: Integer;
  6.   ACodePoint: String;
  7. begin
  8.   CurP := PChar(S);
  9.   EndP := CurP + length(S);
  10.   while CurP < EndP do
  11.   begin
  12.     Len := UTF8CodepointSize(CurP);
  13.     SetLength(ACodePoint, Len);
  14.     Move(CurP^, ACodePoint[1], Len);
  15.     WriteLn(ACodePoint);
  16.     inc(CurP, Len);
  17.   end;
  18. end.
« Last Edit: January 31, 2023, 02:22:32 pm by Warfley »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: What are main differences between free pascal and c#?
« Reply #12 on: January 31, 2023, 02:37:55 pm »
In Pascal every C# code is a comment.
Specialize a type, not a var.

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: What are main differences between free pascal and c#?
« Reply #13 on: January 31, 2023, 02:51:33 pm »
20 years ago this code for iterating a string would work perfectly fine, but not anymore:
Code: Pascal  [Select][+][-]
  1. var
  2.   s: String;
  3.   i: Integer;
  4. begin
  5.   s := '€';
  6.   for i := 1 to s.Length do
  7.     WriteLn(s[i]);
  8. end.
Result:
Code: Pascal  [Select][+][-]
  1. Ô
  2. é
  3. ¼
  4.  
The reason for this is that nowerdays Lazarus uses UTF8 Strings, where each codepoint in the Unicode space is represented by up to 4 (theoretically up to 6) bytes. ANd in this example the euro symbol is encoded as 3 bytes
But FPC does not use UTF8, it assumes that source file in default system code page (if there is no $Codepage directive). So nowerdays this code works also, using proper code page of source and console:

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: What are main differences between free pascal and c#?
« Reply #14 on: January 31, 2023, 03:52:51 pm »
But FPC does not use UTF8, it assumes that source file in default system code page (if there is no $Codepage directive). So nowerdays this code works also, using proper code page of source and console:
Yes, or if you are using the LCL which I would assume is true for most programs written with lazarus. Most importantly, this is just an example, most of the time you won't hardcode the string in the code anyway, but read it, either from the GUI, which is UTF8, or from files, which most likely are UTF8 nowadays.

UTF8 is the de-facto standard used everywhere today, so any code that handles strings that come from any outside input must necesarrily be able to handle UTF8

PS: Interestingly, when I remove everything from the program:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var
  3.   s: String;
  4. begin
  5.   s := '€';
  6.   WriteLn(Length(s));
  7. end.
It still outputs 3, so it also uses UTF8 even without a codepage statement

Window 10 64 bit Lazarus 2.2.2 and FPC 3.2.2
« Last Edit: January 31, 2023, 04:03:40 pm by Warfley »

 

TinyPortal © 2005-2018