45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:DELPHI7与C#语法比较介绍

DELPHI7与C#语法比较介绍

2016-09-04 11:52:56 来源:www.45fan.com 【

DELPHI7与C#语法比较介绍

DELPHI and C# Comparison

DELPHI7与C#语法比较
编制:黄焕尧 参考:VB.net and C# Comparison 日期:2005-5-30.

Comments 注释
Data Types数据类
Constants 常量
Enumerations 枚举
Operators 运算

Choices 选择语句
Loops 循环语句
Arrays 数组
Functions 函数
Exception Handling 异常处理

Namespaces 命名空间
Classes / Interfaces
Constructors / Destructors 构造/释构
Objects 对象
Structs 结构

Properties 属性
Delegates / Events
Console I/O
File I/O

DELPHI

C#

Comments注释

// Single line only
{Multiple

line }

*Multiple

line *)

// Single line
/* Multiple
line */
/// XML comments on single line
/** XML comments on multiple lines */

Data Types 数据类型

Value Types 简单类型
Boolean
Byte
Char (example: "A"c)
Word, Integer, Int64
Real ,Single, Double,Real48,Extended,Comp,Currency
Decimal
Tdate,TDateTime

Reference Types
Object
String(ShortString,AnsiString,WideString)

Set, array, record, file,class,class reference,interface
pointer, procedural, variant

var x:Integer;
WriteLine(x); //Prints System.Int32
WriteLine(‘Ingeger’); // Prints Integer

//Type conversion
var numDecimal:Single = 3.5 ;
var numInt:Integer;
numInt :=Integer(numDecimal) // set to 4 (Banker's rounding)
the decimal)

Value Types
bool
byte, sbyte
char (example: 'A')
short, ushort, int, uint, long, ulong
float,double
decimal
DateTime (not a built-in C#type)

Reference Types
object
string

int x;
Console.WriteLine(x.GetType());// Prints System.Int32
Console.WriteLine(typeof(int)); // Prints System.Int32


// Type conversion
double numDecimal = 3.5;
int numInt = (int) numDecimal; // set to3 (truncates decimal)

Constants常量

ConstMAX_STUDENTS:Integer = 25;

const int MAX_STUDENTS = 25;

Enumerations枚举

Type Taction1=(Start, Stop, Rewind, Forward);

{$M+}
Type Status=(Flunk = 50, Pass = 70, Excel = 90);
{$M-}
var a:Taction1 = Stop;
If a <>Start Then WriteLn(ord(a)); // Prints1

WriteLine(Ord(Pass)); // Prints 70

WriteLine(GetEnumName(TypeInfo(Status),Ord(Pass)));// Prints Pass
ShowEnum(Pass); //outputs 70

GetEnumValue(GetEnumName(TypeInfo(Status),’Pass’));//70

enum Action {Start, Stop, Rewind, Forward};
enum Status {Flunk = 50, Pass = 70, Excel = 90};

Action a = Action.Stop;
if (a != Action.Start)
Console.WriteLine(a + " is " + (int) a);// Prints "Stop is 1"

Console.WriteLine(Status.Pass); //
Prints Pass

Operators运算符

Comparison 比较
= < > <= >= <> in as

Arithmetic 算述述运算
+ - * / div
Mod
/ (integer division)
^ (raise to a power) 阶乘

Assignment 赋值分配
:= Inc() Dec() shl shr

Bitwise 位运算
andxor or not shl shr

//Logical
and xor ornot

//String Concatenation
+

Comparison
== < > <= >= !=

Arithmetic
+ - * /
% (mod)
/ (integer division if both operands are ints)
Math.Pow(x, y)

Assignment
= += -= *= /= %= &= |= ^= <<= >>= ++ --

Bitwise
& | ^~ << >>

Logical
&& || !

Note: && and||perform short-circuit logical evaluations

String Concatenation
+

Choices 判断

greeting := IfThen (age < 20, ‘What's up?’, ‘Hello’);


If language = ‘DELPHI’ Then
langType := ‘hymnlan’;

//
If x <> 100 Then
begin
x := x*5 ; y :=x* 2;
end;

//or to break up any long single command use _
If(whenYouHaveAReally <longLine
and itNeedsToBeBrokenInto2> LinesThen
UseTheUnderscore(charToBreakItUp);

'If x > 5 Then
x :=x* y
Else Ifx = 5 Then
x :=x+ y
Else If x < 10 Then
x :=x- y
Else
x :=x/ y ;

Case colorof // 不能为字符串类型
pink, red:
r :=r+ 1;
blue:
b :=b+1;
green:
g :=g+ 1;
Else
Inc(other);
End;

greeting = age < 20 ? "What's up?" : "Hello";


if (x != 100) { // Multiple statements must be enclosed in {}
x *= 5;
y *= 2;
}

No need for _ or : since ; is used to terminate each statement.




if (x > 5)
x *= y;
else if (x == 5)
x += y;
else if (x < 10)
x -= y;
else
x /= y;



switch (color){ // Must be integer or string
case "pink":
case "red": r++; break; // break is mandatory; no fall-through
case "blue": b++; break;
case "green": g++; break;
default: other++; break; // break necessary on default
}

Loops 循环

Pre-test Loops:

While c < 10 do
Inc(c) ;
End

For c = 2 To 10 do
WriteLn(IntToStr(c));

For c = 10 DownTo 2 do
WriteLn(IntToStr(c));

repeat
Inc(c);
Until c = 10;

// Array or collection looping
count names:array[] of String = (‘Fred’, ‘Sue’, ‘Barney’)
For i:=low(name) to High(name) do
WriteLn(name[i]);

DELPHI8开始支持 for … each … 语法

Pre-test Loops:

// no "until" keyword
while (i < 10)
i++;

for (i = 2; i < = 10; i += 2)
Console.WriteLine(i);



Post-test Loop:

do
i++;
while (i < 10);



// Array or collection looping

string[] names = {"Fred", "Sue", "Barney"};
foreach (string s in names)
Console.WriteLine(s);

Arrays 数组

var nums:array[] of Integer = (1, 2, 3)
For i := 0 To High(nums) do
WriteLn(IntToStr(nums[i]))


// 4 isthe index of the last element, so it holds 5 elements
var names:array[0..5] of String; //用子界指定数组范围
names[0] = "David"
names[5] = "Bobby"

// Resize the array, keeping the existing values
SetLength(names,6);



var twoD:array[rows-1, cols-1] of Single;
twoD[2, 0] := 4.5;

var jagged:array[] of Integer =((0,0,0,0),(0,0,0,0));
jagged[0,4] := 5;

int[] nums = {1, 2, 3};
for (int i = 0; i < nums.Length; i++)
Console.WriteLine(nums[i]);


// 5 is the size of the array
string[] names = new string[5];
names[0] = "David";
names[5] = "Bobby"; // Throws System.IndexOutOfRangeException


// C# doesn't can't dynamically resize an array. Just copy into new array.
string[] names2 = new string[7];
Array.Copy(names, names2, names.Length); // or names.CopyTo(names2, 0);

float[,] twoD = new float[rows, cols];
twoD[2,0] = 4.5f;

int[][] jagged = new int[3][] {
new int[5], new int[2], new int[3] };
jagged[0][4] = 5;

Functions函数

' Pass by value(传值参数) (in, default), 传递引用参数reference (in/out), andreference (out)
procedure TestFunc( x:Integer, var y:Integer, var z:Integer)
;
begin
x :=x+1;
y :=y+1;
z := 5;
End;

count a = 1;
var b:integer=1; c :Integer=0; // cset to zero by default
TestFunc(a, b, c);
WriteLn(Format(‘%d %d %d’,[a, b, c]); // 12 5

// Accept variable number of arguments
Function Sum(nums:array[] of Integer):Integer;
begin
Result := 0;
For i:=Low(nums) to high(nums) do
Result := Result + I;

End

var total :Integer;

total := Sum(4, 3, 2, 1); // returns 10

<p class="MsoNormal" style="TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: aut

本文地址:http://www.45fan.com/dnjc/72174.html
Tags: 比较 语法 Delphi7
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部