Difference between revisions of "Pascal for CSharp users"

From Lazarus wiki
Jump to navigationJump to search
(19 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{|   border="1" style="margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px  solid; border-collapse: collapse;"
+
==Syntax Comparison==
!   C#  
+
{| class="wikitable"
!   [[Pascal]]  
+
! C# !! [[Pascal]] !! Additional comments
!   Additional comments
 
 
|-
 
|-
 
|   {  
 
|   {  
Line 83: Line 82:
 
|   //
 
|   //
 
|   End of line comment (only one line comment)  
 
|   End of line comment (only one line comment)  
 +
|-
 +
|   #define
 +
|   {$Define }
 +
|  
 +
|-
 +
|   #define
 +
|   {$SetC }
 +
|   Mac Pascal
 +
|-
 +
|   #elif
 +
|  
 +
|  
 +
|-
 +
|   #else
 +
|   {$Else}
 +
|  
 +
|-
 +
|   #else
 +
|   {$ElseC }
 +
|   Mac Pascal
 +
|-
 +
|   #endif
 +
|   {$EndIf }
 +
|  
 +
|-
 +
|   #endregion
 +
|  
 +
|  
 +
|-
 +
|   #error
 +
|   {$Fatal }
 +
|  
 +
|-
 +
|   #if
 +
|   {$If }
 +
|  
 +
|-
 +
|   #if
 +
|   {$IfC }
 +
|   Mac Pascal
 +
|-
 +
|   #ifdef
 +
|   {$IfDef }
 +
|  
 +
|-
 +
|   #ifndef
 +
|   {$IfNDef }
 +
|  
 +
|-
 +
|   #pragma
 +
|   {$IfOpt }
 +
|  
 +
|-
 +
|   #region
 +
|  
 +
|  
 +
|-
 +
|   #undef
 +
|   {$Undef }
 +
|  
 +
|-
 +
|   #warning
 +
|   {$Hint }
 +
|  
 
|-
 
|-
 
|   public static void Main(string[] args) { }
 
|   public static void Main(string[] args) { }
|   [[Program]] ProgramName; [[Begin]] [[End]].
+
|   [[Program]] ; [[Begin]] [[End]].
 
|   Note the period after End
 
|   Note the period after End
 
|-
 
|-
|   someType arrayVar[];
+
|   [];
|   arrayVar: [[Array]] [[Of]] someType;
+
|   : [[Array]] [[Of]] ;
 
|  
 
|  
 
|-
 
|-
|   someType arrayVar[#];
+
|   [#];
|   arrayVar: [[Array]][MINRANGE..MAXRANGE] [[Of]] someType;
+
|   : [[Array]][#..#] [[Of]] ;
 
|  
 
|  
 
|-
 
|-
Line 108: Line 171:
 
|  
 
|  
 
|-
 
|-
|   class TheClass { }
+
|   class { }
|   TheClass = [[Class]] [[End]];
+
|   = [[Class]] [[End]];
 
|   Delphi OOP
 
|   Delphi OOP
 
|-
 
|-
|   class TheClass { }
+
|   class { }
|   TheClass = [[Object]] [[End]];
+
|   = [[Object]] [[End]];
 
|   Turbo Pascal OOP
 
|   Turbo Pascal OOP
 
|-
 
|-
| &nbsp; class TheClass<T> { }
+
| &nbsp; class <T> { }
| &nbsp; [[Generic]] TheClass = [[Class]]<T> [[End]];
+
| &nbsp; [[Generic]] = [[Class]]<T> [[End]];
 
| &nbsp; Generics are classes only as of 2.2.2, likely to support more in the future
 
| &nbsp; Generics are classes only as of 2.2.2, likely to support more in the future
 
|-
 
|-
| &nbsp; TheClass()
+
| &nbsp; Class()
| &nbsp; [[Constructor]] CtorName
+
| &nbsp; [[Constructor]]
 
| &nbsp; Constructor name is by convention either Init or Create
 
| &nbsp; Constructor name is by convention either Init or Create
 
|-
 
|-
Line 136: Line 199:
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
| &nbsp; enum TheEnum&nbsp;
+
| &nbsp; enum { } &nbsp;
| &nbsp; TheEnum = ( MINVALUE .. MAXVALUE ); &nbsp;
+
| &nbsp; = (# .. #); &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
| &nbsp; enum TheEnum = {MINVALUE, MAXVALUE}&nbsp;
+
| &nbsp; enum = { = #, }&nbsp;
| &nbsp; TheEnum = ( MINVALUE, MAXVALUE ); &nbsp;
+
| &nbsp; = ( = #, ); &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
 
| &nbsp; TheEnum enumVar; &nbsp;
 
| &nbsp; TheEnum enumVar; &nbsp;
| &nbsp; enumVar := [[Set]] [[Of]] TheEnum; &nbsp;
+
| &nbsp; := [[Set]] [[Of]] ; &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
| &nbsp; Subclass : BaseClass
+
| &nbsp; class :  
| &nbsp; SubClass = [[Class]](BaseClass)
+
| &nbsp; = [[Class]](TObject)
 
| &nbsp;  
 
| &nbsp;  
 
|-
 
|-
Line 156: Line 219:
 
| &nbsp; for constants, not uninheritables
 
| &nbsp; for constants, not uninheritables
 
|-  
 
|-  
| &nbsp; for ++ &nbsp;
+
| &nbsp; for( = ; ; ++) &nbsp;
 
| &nbsp; [[For]] [[To]] [[Do]]
 
| &nbsp; [[For]] [[To]] [[Do]]
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
| &nbsp; for -- &nbsp;
+
| &nbsp; for( = ; ; --) &nbsp;
 
| &nbsp; [[For]] [[Downto]] [[Do]] &nbsp;
 
| &nbsp; [[For]] [[Downto]] [[Do]] &nbsp;
 
| &nbsp;
 
| &nbsp;
 +
|-
 +
| &nbsp; foreach( in ) &nbsp;
 +
| &nbsp; [[For]] [[In]] &nbsp;
 +
| &nbsp; [[for-in_loop|For .. in loop]]
 
|-
 
|-
 
| &nbsp; if() &nbsp;
 
| &nbsp; if() &nbsp;
Line 176: Line 243:
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
| &nbsp; interface
+
| &nbsp; interface { }
| &nbsp; TheInterface = [[Interface]]
+
| &nbsp; = [[Interface]](IInterface) End;
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
| &nbsp; new primitive_type[#];
+
| &nbsp; new [#];
| &nbsp; [[SetLength]](ArrayVar, #);
+
| &nbsp; [[SetLength]]( , #);
 
| &nbsp;  &nbsp;
 
| &nbsp;  &nbsp;
 
|-
 
|-
| &nbsp; new Class();
+
| &nbsp; new ();
| &nbsp; InstanceVar := TheClass.Create;
+
| &nbsp; := .Create;
 
| &nbsp;  &nbsp;
 
| &nbsp;  &nbsp;
 
|-
 
|-
 
| &nbsp; namespace Name { }
 
| &nbsp; namespace Name { }
| &nbsp; [[Unit]] Name; [[Interface]] [[Implementation]] [[End]].
+
| &nbsp; [[Unit]] ; [[Interface]] [[Implementation]] [[End]].
 
| &nbsp; Note the period after End
 
| &nbsp; Note the period after End
 +
|-
 +
| &nbsp; out
 +
| &nbsp; [[Out]]
 +
| &nbsp;
 +
|-
 +
| &nbsp; override
 +
| &nbsp; [[Override]]
 +
| &nbsp;
 
|-
 
|-
 
| &nbsp; private
 
| &nbsp; private
Line 202: Line 277:
 
| &nbsp; public
 
| &nbsp; public
 
| &nbsp; [[Public]]
 
| &nbsp; [[Public]]
 +
| &nbsp;
 +
|-
 +
| &nbsp; ref
 +
| &nbsp; [[Variable_parameter|Var]]
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
Line 211: Line 290:
 
| &nbsp; [[Result]] :=
 
| &nbsp; [[Result]] :=
 
| &nbsp; ObjFPC or Delphi modes
 
| &nbsp; ObjFPC or Delphi modes
 +
|-
 +
| &nbsp; sealed
 +
| &nbsp; sealed
 +
| &nbsp; starting from fpc 2.5.1
 
|-
 
|-
 
| &nbsp; static
 
| &nbsp; static
Line 222: Line 305:
 
| &nbsp; static
 
| &nbsp; static
 
| &nbsp; [[Class]] [[Procedure]]
 
| &nbsp; [[Class]] [[Procedure]]
 +
| &nbsp;
 +
|-
 +
| &nbsp; struct  { }
 +
| &nbsp;  = [[Record]] [[End]];
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
Line 228: Line 315:
 
| &nbsp; Parent constructor call
 
| &nbsp; Parent constructor call
 
|-
 
|-
| &nbsp; switch case break &nbsp;
+
| &nbsp; switch () { case: break; } &nbsp;
| &nbsp; [[Case]] [[Of]] [[End]]  
+
| &nbsp; [[Case]] [[Of]] [[End]] ;
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
| &nbsp; switch case break default &nbsp;
+
| &nbsp; switch() { case: break; default: } &nbsp;
 
| &nbsp; [[Case]] [[Of]] [[Else]] [[End]] &nbsp;
 
| &nbsp; [[Case]] [[Of]] [[Else]] [[End]] &nbsp;
 
| &nbsp;
 
| &nbsp;
Line 246: Line 333:
 
| &nbsp; try { } catch finally
 
| &nbsp; try { } catch finally
 
| &nbsp; [[Try]] [[Finally]]
 
| &nbsp; [[Try]] [[Finally]]
 +
| &nbsp;
 +
|-
 +
| &nbsp; unsafe
 +
| &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; virtual
 +
| &nbsp; [[Virtual]]
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
Line 258: Line 353:
 
| &nbsp; while &nbsp;
 
| &nbsp; while &nbsp;
 
| &nbsp; [[While]] [[Do]]
 
| &nbsp; [[While]] [[Do]]
 +
| &nbsp;
 +
|-
 +
| &nbsp;  &nbsp;
 +
| &nbsp; [[Asm]] [[End]];
 
| &nbsp;
 
| &nbsp;
 
|}
 
|}
  
 
+
==Types Comparison==
{| &nbsp; border="1" style="margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px  solid; border-collapse: collapse;"
+
{| class="wikitable"
! &nbsp; C# type &nbsp;
+
! C# type !! [[Pascal]] [[Data_type|type]] !! Size (bits) !! Range !!
! &nbsp; [[Pascal]] [[Data_type|type]] &nbsp;
 
! &nbsp; Size (bits) &nbsp;
 
! &nbsp; Range &nbsp;
 
! &nbsp;
 
 
|-
 
|-
 
| &nbsp; byte &nbsp;
 
| &nbsp; byte &nbsp;
Line 273: Line 368:
 
| &nbsp; 8-bit
 
| &nbsp; 8-bit
 
| &nbsp; -128 .. 127
 
| &nbsp; -128 .. 127
 +
| &nbsp;
 +
|-
 +
| &nbsp; ubyte &nbsp;
 +
| &nbsp; [[Byte]]
 +
| &nbsp; 8-bit
 +
| &nbsp; 0 .. 255
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
Line 279: Line 380:
 
| &nbsp; 16-bit
 
| &nbsp; 16-bit
 
| &nbsp; -32768 .. 32767 &nbsp;
 
| &nbsp; -32768 .. 32767 &nbsp;
 +
|-
 +
| &nbsp; ushort &nbsp;
 +
| &nbsp; [[Word]]
 +
| &nbsp; 16-bit
 +
| &nbsp; 0 .. 65535
 +
| &nbsp;
 
|-
 
|-
 
| &nbsp; int &nbsp;
 
| &nbsp; int &nbsp;
Line 286: Line 393:
 
| &nbsp;   
 
| &nbsp;   
 
|-
 
|-
| &nbsp; int &nbsp;
+
| &nbsp; uint &nbsp;
| &nbsp; [[Integer]]
+
| &nbsp; [[LongWord]]
 
| &nbsp; 32-bit
 
| &nbsp; 32-bit
| &nbsp; -2147483648..2147483647 &nbsp;
+
| &nbsp; 0..4294967295 &nbsp;
 
| &nbsp;   
 
| &nbsp;   
 
|-
 
|-
Line 296: Line 403:
 
| &nbsp; 64-bit
 
| &nbsp; 64-bit
 
| &nbsp; -9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807  &nbsp;
 
| &nbsp; -9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807  &nbsp;
 +
| &nbsp; 
 +
|-
 +
| &nbsp; ulong &nbsp;
 +
| &nbsp; [[QWord]]
 +
| &nbsp; 64-bit
 +
| &nbsp; 0 .. 18 446 744 073 709 551 615  &nbsp;
 
| &nbsp;   
 
| &nbsp;   
 
|-
 
|-
Line 324: Line 437:
 
| &nbsp; string &nbsp;
 
| &nbsp; string &nbsp;
 
| &nbsp; [[String]]
 
| &nbsp; [[String]]
 +
| &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; datetime &nbsp;
 +
| &nbsp; [[TDateTime]]
 
| &nbsp;  
 
| &nbsp;  
 
| &nbsp;
 
| &nbsp;
Line 329: Line 447:
 
|-
 
|-
 
|}
 
|}
 +
 +
==C# Language Features Not available in Pascal==
 +
* automatic memory management while - not available, except for reference counted types: strings (except for shortstrings), dynamic arrays
 +
* lambda functions
 +
* serialization (as syntax language) - you'd need to use explicit functions/procedures to convert to/from data structures
 +
* LINQ
 +
 +
==FPC Language Features not available in C #==
 +
* [[Assembler]], [[Assembly language]]
 +
* Multi-Platform Support
 +
* Case-insensitive
 +
[[Category:FPC]]
 +
[[Category:Pascal]]
 +
[[Category:C-Sharp]]

Revision as of 13:34, 21 October 2018

Syntax Comparison

C# Pascal Additional comments
  {     Begin    
  }     End  
  =     :=   Becomes  
  ==     =   Equal
  /     /   Division (or sometimes div)  
  %     Mod   Modulo operation
  !     Not  
  !=     <>   Not equal  
  &&     And  
  ||     Or  
  ^     Xor  
  >>     Shr   bit shift right  
  <<     Shl   bit shift left  
  ++     Inc  
  --     Dec  
  /*     {   Comment start
  /*     (*   Comment start
  */     }   Comment end
  */     *)   Comment end
  //     //   End of line comment (only one line comment)  
  #define   {$Define }  
  #define   {$SetC }   Mac Pascal
  #elif    
  #else   {$Else}  
  #else   {$ElseC }   Mac Pascal
  #endif   {$EndIf }  
  #endregion    
  #error   {$Fatal }  
  #if   {$If }  
  #if   {$IfC }   Mac Pascal
  #ifdef   {$IfDef }  
  #ifndef   {$IfNDef }  
  #pragma   {$IfOpt }  
  #region    
  #undef   {$Undef }  
  #warning   {$Hint }  
  public static void Main(string[] args) { }   Program ; Begin End.   Note the period after End
  [];   : Array Of ;  
  [#];   : Array[#..#] Of ;  
  null   Nil  
  abstract   Abstract  
  break   Break  
  class { }   = Class End;   Delphi OOP
  class { }   = Object End;   Turbo Pascal OOP
  class <T> { }   Generic = Class<T> End;   Generics are classes only as of 2.2.2, likely to support more in the future
  Class()   Constructor   Constructor name is by convention either Init or Create
  continue   Continue  
  do while     Repeat Until Not   
  do while !     Repeat Until    
  enum { }     = (# .. #);    
  enum = { = #, }    = ( = #, );    
  TheEnum enumVar;     := Set Of ;    
  class :   = Class(TObject)  
  const   Const   for constants, not uninheritables
  for( = ; ; ++)     For To Do  
  for( = ; ; --)     For Downto Do    
  foreach( in )     For In     For .. in loop
  if()     If Then  
  if() else     If Then Else  
  using   Uses  
  interface { }   = Interface(IInterface) End;  
  new [#];   SetLength( , #);    
  new ();   := .Create;    
  namespace Name { }   Unit ; Interface Implementation End.   Note the period after End
  out   Out  
  override   Override  
  private   Private  
  protected   Protected  
  public   Public  
  ref   Var  
  return   FunctionName :=  
  return   Result :=   ObjFPC or Delphi modes
  sealed   sealed   starting from fpc 2.5.1
  static   Static  
  static   Class Function  
  static   Class Procedure  
  struct { }   = Record End;  
  base   Inherited   Parent constructor call
  switch () { case: break; }     Case Of End ;  
  switch() { case: break; default: }     Case Of Else End    
  this   Self  
  try { } catch   Try except  
  try { } catch finally   Try Finally  
  unsafe    
  virtual   Virtual  
  void   Procedure  
  volatile    
  while     While Do  
      Asm End;  

Types Comparison

C# type Pascal type Size (bits) Range
  byte     Shortint   8-bit   -128 .. 127  
  ubyte     Byte   8-bit   0 .. 255  
  short     SmallInt   16-bit   -32768 .. 32767  
  ushort     Word   16-bit   0 .. 65535  
  int     LongInt   32-bit   -2147483648..2147483647    
  uint     LongWord   32-bit   0..4294967295    
  long     Int64   64-bit   -9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807    
  ulong     QWord   64-bit   0 .. 18 446 744 073 709 551 615    
  float     Single   32-bit   1.5E-45 .. 3.4E+38  
  double     Double   64-bit   5.0E-324 .. 1.7E+308  
  bool     Boolean     False True  
  char     WideChar   16-bit    
  string     String    
  datetime     TDateTime    

C# Language Features Not available in Pascal

  • automatic memory management while - not available, except for reference counted types: strings (except for shortstrings), dynamic arrays
  • lambda functions
  • serialization (as syntax language) - you'd need to use explicit functions/procedures to convert to/from data structures
  • LINQ

FPC Language Features not available in C #