IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesC#Language Basics  Print This     

Cross Ref > Language Basics

By Mike Prestwood

C# versus Delphi: A side by side comparison between C# and Delphi.

 
Language Basics
 

Language basics is kind of a catch all for absolute beginner stuff. The items (common names) I chose for language basics is a bit random and include items like case sensitivity, commenting, declaring variables, etc.

Case Sensitivity

[Other Languages] 

Languages Focus

Case sensitiviy in this case is referring to commands and variable names. For example, are "printf" and "PrintF" equivalent? Are fullname and FullName equivalent? When you create commands, operations, methods, or variables should you worry about case?

C#:   Yes

In C# commands and variable names are case sensitive. The following does NOT:

messagebox.Show("hello");  //Does not compile!

The first time you type any other case for commands or variables, VS.Net will change it to the accepted or defined case. For example, if you type messagebox.show it is converted to MessageBox.Show. Once corrected, you can break it again by editing MessageBox to messagebox and the compiler will give you an error.

Syntax Example:

The following code works:

MessageBox.Show("hello");
Delphi:   No

Object Pascal is generally not case sensitive.

Syntax Example:  

Variables and commands are not case sensitive.

var
FullName: String;
begin
fullname := 'Mike Prestwood';
ShowMessage(fullNAME);
SHOWMESSAGE(FULLNAME);
showmessage(fullname);
end;




Code Blocks

[Other Languages] 

Languages Focus

The rules for code blocks within a language dictate where you can declare variables, how you "bracket" code, etc.

C#:   { }

C# uses braces {} to indicate a code block of more than one line. For one line of code, the braces are optional.

I prefer to put the opening { and the closing } on their own line only because most of the examples I see do this. As opposed to C++, Java, and JavaScript where I put the opening bracket at the end of the first line (which I actually prefer).

Syntax Example:
int DoSomething()
{
 int a = 1;
 int b = 2;
 return a + b;
}
Delphi:   begin..end

Object Pascal requires the semi-colon after the "declaration" part.

Syntax Example:
function DoSomething : integer;
var
  a, b : integer
begin
  a := 1;
  b := 2;
  
  result := a + b;
end;




Comments

[Other Languages] 

Languages Focus

Commenting code generally has three purposes: to document your code, for psuedo coding prior to coding, and to embed compiler directives. Most languages support both a single line comment and a multiple line comment. Some languages also use comments to give instructions to the compiler or interpreter.

C#:  "Multiple Line Comment" // or /* */

Commenting Code
C# uses "//" for a single line comment and /* */ for a multiple line comment.

Syntax Example:
//Single line comment.

/*
Multiple line
comment.
*/
Delphi:   // or { ... } or (* ... *)

Commenting Code
Delphi uses // for a single line comment and both {} and (**) for multiple line comments. Although you can nest different types of multiple line comments, it is recommended that you don't.

Compiler Directives - $
A special comment. Delphi compiler directives are in the form of {$DIRECTIVE}. Of interest for comments is using the $IFDEF compiler directive to remark out code.

Syntax Example:
//This is a single line comment.
 
{
Multiple line
comment.
}
 
(*
This too is a
multiple line comment.
*)
 
{$IFDEF TEMPOUT}
//...code here
{$ENDIF}




Constants

[Other Languages] 

General Info: Computer Language Constants

A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

C#:   const

In C#, you define constants with the const keyword.

All constants are part of a class (no global constants) but you can make a constant public and have access to it using ClassName.ConstantName so long as you have added the class to the project. This works even without creating the class as if the public constants were static, but you cannot use the static keyword.

Constants must be of an integral type (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or string), an enumeration, or a reference to null.

Syntax Example:  
public class Convert : Object
{
  public const string kName = "Mike";
 
  //You can declare multiple of the same type too:
  const Double kFeetToMeter = 3.2808, kMeterToFeet = .3048;
}
Delphi:   Const kPI: Double=3.1459;

In Delphi, you define constants similar to how you define variables but use the Const keyword instead of the Var keyword. Declare global constants in a unit's interface section and unit constants (scope limited to unit) in the implementation section. Declare local constants above the begin..end block.

Syntax Example:
Const 
  kFeetToMeter: Double = 3.2808;
  kMeterToFeet: Double = .3048;
  kName: String = "Mike";
 
//Local constants:
procedure SomeProcedure;
const
  kPI: Double=3.1459;
begin
end;




End of Statement

[Other Languages] 

Languages Focus

In coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.

C#:   ;

C# uses a semicolon ";" as an end of statement specifier and you can put multiple statements on a single line of code if you wish as well as split a single statement into two or more code lines.

Syntax Example:  
Console.WriteLine("Hello1");
Console.WriteLine("Hello2");
Console.WriteLine("Hello3");

//Same line works too:
Console.WriteLine("Hello4"); Console.WriteLine("Hello5");
 
//Two or more lines works too: 
Console.
  WriteLine
("Hello6");

Delphi:   ;

Object Pascal uses a semicolon ";" as an end of statement specifier and you can put multiple statements on a single line of code and put a single statement on two or more code lines if you wish.

Syntax Example:  
WriteLn('Hello1');
WriteLn('Hello2');
WriteLn('Hello3');

//Same line works too:
WriteLn('Hello4'); WriteLn('Hello5');

//Two or more lines works too:
WriteLn
('Hello6');




Literals

[Other Languages] 

General Info: Programming Literals

A value directly written into the source code of a computer program (as opposed to an identifier like a variable or constant). Literals cannot be changed. Common types of literals include string literals, floating point literals, integer literals, and hexidemal literals. Literal strings are usually either quoted (") or use an apostrophe (') which is often referred to as a single quote. Sometimes quotes are inaccurately referred to as double quotes.

Languages Focus

In addition to understanding whether to use a quote or apostrophe for string literals, you also want to know how to specify and work with other types of literals including floating point literals. Some compilers allow leading and trailing decimals (.1 + .1), while some require a leading or trailing 0 as in (0.1 + 0.1). Also, because floating point literals are difficult for compilers to represent accurately, you need to understand how the compiler handles them and how to use rounding and trimming commands correctly for the nature of the project your are coding.

C#:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding decimals are allowed (both .1 and 0.1). Trailing decimals are not allowed.

Syntax Example:
Console.WriteLine("Hello");
Console.WriteLine("Hello \"Mike\".");
 
//Does C# evaluate this simple
//floating point math correctly? No! 
if ((0.1 + 0.1 + 0.1) == 0.3)
{
MessageBox.Show("Correct");
}
else
{
MessageBox.Show("Not correct");
}
Delphi:   apostrophe

String literals are single quoted (the apostrophe) as in 'Prestwood'. If you need to embed an apostrophe use two apostrophes in a row.

Floating point literals must start with an integer. For example, to specify a fractional floating point literal between 1 and -1, you preceed the decimal with a 0; otherwise, you will get a compiler error.

//x := .1 + .1;   //Does not work.
x := 0.1 + 0.1;   //Does work.
Syntax Example:  
ShowMessage('Hello');
ShowMessage('Hello Mike''s website.');




Variables

[Other Languages] 

Languages Focus

A variable holds a value that you can use and change throughout your code so long as the variable is within scope. With variable declaration, you not only want to know the syntax of how you declare a variable but you also want to know where. Are you allowed to declare a variable inline? What are the available scopes: local vs. global. Can you assign a value at the same time you declare a variable?

C#:   Int16 x=0;

C++, Java, and C# all use C-like variable declaration.

C# has C-like variable declaration and although variables are case sensitive, VS.Net will auto-fix your variable names to the defined case.

C# offers many variable types. Some common types used include short, intlong, float, double, decimal, Int16, UInt16, Int32, Int64, string, and bool.

You can also specify the value when you declare a variable as in:

String FirstName = "Mike";
String LastName = "Prestwood";
Int16 Age = 42;
Syntax Example:
string FullName;
int16 Age;
double Weight;
 
FullName = "Mike Prestwood";
Age = 32;
Weight = 154.4;
Delphi:   var x: Integer = 0;

The Delphi language is a strongly typed language so you have to specifically declare variables and frequently use commands such as IntToStr and StrToInt.

Declare global variables in the interface section of a unit, variables declared within the implementation section (but not within a method) have a scope limited to the unit. You declare local variables in a var block outside (above) your begin..end code block. You cannot declare variables in-line (inside begin..end).

You can initialize global and unit variables but you cannot initialize local variables.

Delphi offers many variable types. Some common variable types include String, WideString, PCharInteger, Boolean, Single, Double, Pointer, and Variant.

Note: D2009 introduced a new UnicodeString type.

Syntax Example:
procedure SomeProcedure;
var
  Fullname: String;
  Age: Integer;
  X, Y, Z: Double;
  MyArray: array [1..100] of Char;
begin
end;
 
//You can initialize global variables.
var
  ClickCounter: Integer = 0;




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]