Comparison Table for Pascal and C#

Language Feature Pascal C#
Statement separator ; ; (required to terminate each statement, even before else)
Comment

//Comment after code
{Comment spanning
lines}

//Comment after code
/* Comment spanning
lines */

Quoted string

'Hello World'

"Hello World"

Declaring constants const VAT_RATE = 0.2; const Single VAT_RATE = 0.2
Declaring variables

var
  forename, surname : string;
  paysTax : Boolean;
  totalHits : integer;
  averageScore : double;
               //same as real
  initial: char;
               //same as ansiChar    
  months : array [1..12] of
           string;
  rainfall : array [2004 .. 2009,
              1..12] of integer;
  ageRecords : array[1..1000] of
               TRec;


Also  wideChar,  byte,  single, currency,  TDateTime ...


String forename, surname;
Boolean paysTax;
Integer totalHits;
Double averageScore;

Char initial; //Unicode 16-bit  

String[] months = new String[11];
  
Integer[,] rainfall = new Integer[11, 6];
ageRecord[] ageRecords = new ageRecord[999];
//where ageRecord has been declared as a struct


Also Byte, short, Decimal, DateTime ...

Assigning variables

surname := 'Morrison';
averageScore := 24.56;
paysTax := True;
initial := 'N';
dateSubmitted := StrToDateTime(
                  '19/4/2014');
months[1] := 'January';
rainfall[2000, 1] := 15; // or  
rainfall[2000][1] := 15;

surname = "Morrison";
averageScore = 24.56;
paysTax = true; //lower case t
initial = 'N';  //Single quotes  
dateSubmitted = new DateTime(
                 2014, 4, 19);

months[0] = "January";
rainfall[2000, 1] = 15; // or  
rainfall[2000][1] = 15;

If statement
  One statement
  executed if true



  More than one
  statement
  executed if true


if (currentNum > max) then
  max := currentNum
else
  writeln('max still ', max);


if (currentNum > max) then
  begin
    max := currentNum;
    writeln('max increased');
  end
else
  write('max still ', max);


if (currentNum > max)
  max = currentNum;
else
  Console.WriteLine("max still " +
                    max.ToString());

if (currentNum > max)
{

  max = currentNum;
  Console.WriteLine(
               "max increased");
}

else
  Console.WriteLine("max still " +
                    max.ToString());

Case/Switch statement

case iChoice of
  1 : DoThis;
  2 : DoThat;
end;

switch(iChoice)
{
  case 1: DoThis();
          break;
  case 2: DoThat();
          break;
}

For loop

for i := 1 to 10 do
  begin
    lines of code
  end;
  
for i := 10 downto 1 do
  begin
    lines of code
  end;

for (Integer i = 1; i <= 10; i++)
{
   lines of code
}

for (Integer i = 10; i >= 1; i--)
{
   lines of code
}
//also
foreach (Integer i in myList)
{
  Console.WriteLine(i);
}

While loop

while amount > 0 do
  begin
    lines of code
  end;

while (amount > 0)
{
  lines of code
}

Repeat loop

repeat
  lines of code
until finished = true;

do
{
  lines of code
}
while (!finished)

Defining records/structs

type
  TAgeRec = record
    surname : string;
    age : integer;
  end;

public struct ageRecord
{
  public String surname;
  public Integer age;
}

Operators for:
  addition
  subtraction
  multiplication
  real division
  integer division
  remainder


+
-
*
/
DIV
MOD


+
-
*
/ (with a real operand)
/ (with Integer operands)
%

Procedure without parameters

procedure ListRecords;
begin
  lines of code
end;


To call the procedure:
ListRecords;

public void ListRecords()
{
  lines of code
}


To call the procedure:
ListRecords();

Procedure with parameter passed by value

procedure ListRec(recNo: Integer);
begin
  lines of code
end;


To call the procedure for the fifth record:
ListRec(5);

public void ListRec(Integer recNo)
{
  lines of code
}
                  
                  
To call the procedure for the fifth record:
ListRec(5);

Procedure with parameter passed by reference

procedure Calc(var total :
              integer);
begin
  lines of code
end;

public void Calc(ref Integer total)
{
  lines of code
}

//call with e.g.
Calc(ref score);

Function

function Cube(Num: double) : double;
begin
  Cube := Num * Num * Num;
end;

public Double Cube(Double num)
{
  return num * num * num;
}

Inbuilt string functions
  Type conversions
                  
                  
  


  String manipulation


IntToStr converts integer to string
FloatToStr converts real to string
FloatToStrf converts real to formatted string
DateTimeToStr converts from TDateTime to string
StrToInt converts string to integer  
                  

Length returns length of  string



Pos looks for one string within another
and returns the position of the first matching character.

(StrUtils Unit)
MidStr returns the required number of characters
from any position in the string
LeftStr returns the required number of characters
from the front of the string
RightStr returns the required number of characters
from the back of the string
UpperCase  returns a string in upper case
LowerCase returns a string in lower case


ToString() converts numeric value to string e.g.
max.ToString()


String strNum = "5";
Integer intNum = Convert.ToInt32(strNum);

String alphabet =
     "abcdefghijklmnopqrstuvwxyz";
Integer stringLength = alphabet.Length;
                  
Integer jklPos = alphabet.IndexOf("jkl");
if (jklPos != -1)
  Console.WriteLine(
               "jkl position: " +
               jklPos.ToString());

                
alphabet.Substring(9, 3)

alphabet.Substring(0, 3)

alphabet.Substring(
               alphabet.Length - 3, 3)
                  
alphabet.ToUpper()
forename.ToLower()

Writing a text file

var MyFile : TextFile;
begin
  assignFile (MyFile, 'F:\line.txt');
  rewrite (MyFile);
  writeln('One line only');
  closeFile (MyFile);
end;

(Using System.IO)

StreamWriter sw = new StreamWriter(
               "F:line.txt");  
sw.WriteLine(
               "One line only");      
sw.Close();

Reading a text file

var
  line : String;
  myFile : TextFile;
begin
  assignFile(myFile, 'F:\line.txt');
  reset(myFile);
  readln(myFile, line);
  closeFile(MyFile);
end;

(Using System.IO and shows how to read the whole contents not just one line as in this file)

StreamReader sr = new
      StreamReader("F:line.txt");
String line = "";
while (line != null)
{
  line = sr.ReadLine();
  Console.WriteLine(line);
}
sr.Close();

Programming - a skill for life!

How to apply your knowledge of Pascal when learning C#