I.T. Discussion Community!
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesDelphi  Print This     

Variables (Delphi and C# Cross Reference Guide)

By Mike Prestwood

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

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.


[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?

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;
  Fullname: String;
  Age: Integer;
  X, Y, Z: Double;
  MyArray: array [1..100] of Char;
//You can initialize global variables.
  ClickCounter: Integer = 0;
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;

Follow PrestwoodBoards on: 

©1995-2020 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech