Variant parts in records

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Record > Variant parts in records

 

C# structures cannot have variant parts as Delphi records can have. However by means of the StructLayout(LayoutKind.Explicit) and FieldOffset attributes, the behavior can be reproduced.

 

 

  TRect = packed record

    case Integer of

      0: (Left, Top, Right, Bottom: Longint);

      1: (TopLeft, BottomRight: TPoint);

  end;

 

  ->

 

[StructLayout(LayoutKind.Explicit)]

public struct TRect

{

  /*# 0*/

  [FieldOffset(0)]

  public int Left;

  [FieldOffset(4)]

  public int Top;

  [FieldOffset(8)]

  public int Right;

  [FieldOffset(12)]

  public int Bottom;

  /*# 1*/

  [FieldOffset(0)]

  public TPoint TopLeft;

  [FieldOffset(4)]

  public TPoint BottomRight;

  public static TRect CreateRecord(){return new TRect();}

};

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content