.NET
•  Greatis •  Security •  AppDatabase •  Utilities •  Delphi/CB • Visual Basic •  just4fun
.NET Software
.NET Components for Visual Studio .NET, Delphi 8, Delphi 2005 and Delphi 2006
 .NET     Entire site
More Info
Delphi to C#
Index

.NET
Form Designer .NET

See also
Delphi VCL software
Visual Basic 6 software
Hot Offer

Form Desigber .Net

Delphi to C#
Language
Classes and objects

  Delphi  

       Class declared as local or global type, derived from some ancestor class. Class declaration can have private, protected, public and published sections:
type
  TNewClass = class(TSomeAncestor)
  private
    // some private declarations
  protected
    // some protected declarations
  public
    // some public declarations
  published
    // some published declarations
  end;
Private declarations are visible only within this class and within this unit, protected declarations are visible within this class and within its descendants, public declarations are visible wherever this class can be referenced, and published declarations has the same visibility like public and are shown in the IDE's Object Inspector in addition.

All classes are inherited from TObject, so if the ancestor is not specified, class is derived from TObject. The following declarations are identical:

TNewObject = class(TObject)
TNewObject = class
Class can have virtual, dynamic and message methods. Virtual and dynamic methods are similar and just processed by another systems: VMT (Virtual Method Table - more memory, faster calls) and DMT (Dynamic Method Table - less memory, slower calls). Message methods are called by message index when object processes the Windows messages. Virtual and dynamic methods overridden with override directive, message methods just use the message directive:
type
  TMyObject = class(TWinControl)
  protected
    procedure VirtualMethod; virtual;
    procedure DynamicMethod; dynamic;
    procedure MessageMethod(var Msg: TMessage); message WM_SOMEWINDOWSMESSAGE;
    ...
  end;

type
  TMyNewObject = class(TMyObject)
  protected
    procedure VirtualMethod; override;
    procedure DynamicMethod; override;
    procedure MessageMethod(var Msg: TMessage); message WM_SOMEWINDOWSMESSAGE;
    ...
  end;
Delphi adds another new feature of class - property. Property is a field with some access methods. There is special kind of property - event. In fact, each property of procedure/function of object type is an event. When properties and events are published, they are shown in IDE's Object Inspector.
type
  TDataChangeEvent = procedure(Sender: TObject; AData: string) of object;
  TMyObject = class(TWinControl)
  private
    FData: string;
    FFlags: Integer;
    FOnDataChange: TDataChangeEvent;
    // access methods for Data property
    function GetData: string;
    procedure SetData(const Value: string);
  published
    // access with methods
    property Data: string read GetData write SetData;
    // direct access
    property Flags: Integer read FFlags write FFlags;
    // event declaration
    property OnDataChange: TDataChangeEvent read FOnDataChange write FOnDataChange;
  end;
When write operator is missed in the property declaration, the property is read only:
property Flags: Integer read FFlags;
Object is just an instance of class. Each object can be created by calling constructor (usually Create) and destroyed by calling the Free procedure which checks the object pointer and if it is not nil, calls the Destroy destructor:
type
  TMyObject = class(TPersistent)
    ...
  end;

var
  MyObject: TMyObject;
...
  // creating MyObject
  MyObject:=TMyObject.Create;
  // working with MyObject
  ...
  // destroying MyObject
  MyObject.Free;
Within its methods, object can be accessed by the predefined Self variable:
TMyObject.SomeMethod;
begin
  SomeExternalCallForProcessingThisClass(Self);
end;
Class and object fields, methods, properties and events can be accessed with easy using with operator. Compare two following parts:
// with is not used
Form1.Button1.Left:=20;
Form1.Button1.Top:=10;
Form1.Button1.Font.Size:=8;
Form1.Button1.Font.Name:='Tahoma';

// with is used
with Form1.Button1,Font do
begin
  Left:=20;
  Top:=10;
  Size:=8;
  Name:='Tahoma';
end;
 
 
  C#

Everything is a class. Nothing can be created without class. You cannot create some non-class procedures and functions, you can create any functionality only as class methods.

Like in Delphi only single inheritance is allowed in C#. In other words, a class can inherit implementation from one base class only. However, a class can implement more than one interface. You can also declare generic classes that have type parameters.

A class can contain declarations of the following members:
Constructors
Destructors
Constants
Fields
Methods
Properties
Indexers
Operators
Events
Delegates
Classes
Interfaces
Structs
Class members can be marked as public, private, protected, internal, or protected internal.

Private members are accessible only within the body of the class or the struct in which they are declared.
A protected member is accessible within its class and by derived classes.
Internal types or members are accessible only within files in the same assembly.
Public access is the most permissive access level. There are no restrictions on accessing public members.

All classes are inherited from Object, so if the ancestor is not specified, class is derived from Object.

A field can optionally be declared static. This makes the field available to callers at any time, even if no instance of the class exists. Also field can be declared readonly. A read-only field can only be assigned a value during initialization or in a constructor. A static readonly field is very similar to a constant.

Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as though they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily while still providing the safety and flexibility of methods.

A method is a code block containing a series of statements.

A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature.

Events are a way of providing notifications about occurrences, such as button clicks or the successful completion of a method, to other objects. Events are defined and triggered using delegates.

Operators are terms or symbols such as +, *, <, and so on that perform operations on operands. Operators can be redefined to perform operations on custom data types.

Indexers allow an object to be indexed in a manner similar to arrays.

Constructors are methods that are called when the object is first created. They are often used to initialize the object's data.

Destructors are methods that are called by the runtime execution engine when the object is about to be removed from memory. They are generally used to make sure that any resources which need to be released are handled appropriately.

Nested Types (classes, interfaces and structs) are types declared within a class. Nested Types are often used to describe objects used only by the types containing them.

Class are separated into value types and reference types. Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated. In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it. The wrapper is marked so the system knows that it contains a value type. This process is known as boxing, and the reverse process is known as unboxing. Boxing and unboxing allow any type to be treated as an object.

Within its methods, object can be accessed by the predefined this variable:

class Test
{
   private int a;

   Test(int val)
   {
      this.a = val;
   }
}

Lost features      All functionality can be created within class methods only, you cannot create external functions and procedures. Very useful with has no analogue in C#.
Added features You can redefine any operators in your own class to simplify the application code.
Warnings
Additional information

 
 


Greatis Software Greatis | Security | AppDatabase | Utilities | Delphi/CB | Visual Basic | .NET | just4fun

Contacts | Add to Favorites | Recommend to a Friend | Privacy Policy | Copyright © 1998-2009 Greatis Software

eXTReMe Tracker