|
Delphi |
|
Record is a set of heterogeneous items:
var
Rec = record
I: Integer;
S: string;
end;
You can define the record type and use it for defining the record variables:
type
RecType: record
I: Integer;
S: string;
end;
var
R1,R2: RecType;
Record can have variant part that allows to store different data (the case tag and constants in the list play no role):
var
XData: record
Name: string;
case Integer of
0: (Price: Integer);
1: (Enabled: Boolean);
2: (CatalogItem: Char);
end;
Record fields can be accessed by the field name:
XData.Name:='Form Designer .NET';
XData.Price:=200;
This code can be shorted by the with statement:
with XData do
begin
Name:='Form Designer .NET';
Price:=200;
end;
The global record variables can be preinitialized:
var
XData: record
Name: string;
case Integer of
0: (Price: Integer);
1: (Enabled: Boolean);
2: (CatalogItem: Char);
end = (Name:'Form Designer .NET'; Price:200);
|
|
C# |
|
In C# struct is almost class. Structure cannot be inherited - it's the only difference between class and struct. Structure can have fields with any access modifier, but protected. Structure can have constructors, constants, methods, events and so on. In fact it's a non-inheritable class, that can implement interfaces. Too compilcated, isn't it?
|
|
You can have rich functionality within stuct. |
|
|
|
|
|
|
|
Be careful when choosing between class and struct. |
|
|
|
|
|
|
|
|
|
|
|