Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Delphi和可空类型_C#_Delphi - Fatal编程技术网

C# Delphi和可空类型

C# Delphi和可空类型,c#,delphi,C#,Delphi,我正在将一些C#代码转换为Delphi 在c#中,我将Nullabe类型设置为: System.Nullable<T> variable 然后,我可以使用以下内容: int?[] arr = new int?[10]; 是否有与之相当的Delphi VCL?我发现了一篇关于可空类型和简化实现的有趣文章: unit Nullable; {$mode delphi}{$H+} interface uses Classes, SysUtils; type TNullab

我正在将一些C#代码转换为Delphi

在c#中,我将Nullabe类型设置为:

System.Nullable<T> variable
然后,我可以使用以下内容:

int?[] arr = new int?[10];
是否有与之相当的Delphi VCL?

我发现了一篇关于可空类型和简化实现的有趣文章:

unit Nullable;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils;

type
  TNullable<T> = record
  private
    FHasValue: Boolean;
    FValue: T;
    function GetValue: T;
    procedure SetValue(AValue: T);
  public
    procedure Clear;
    property HasValue: Boolean read FHasValue;
    property Value: T read GetValue write SetValue;
    class operator Implicit(A: T): TNullable<T>;
    class operator Implicit(A: Pointer): TNullable<T>;
  end;

implementation

{ TNullable }

function TNullable<T>.GetValue: T;
begin
  if FHasValue then
     Result := FValue
  else
    raise Exception.Create('Variable has no value');
end;

procedure TNullable<T>.SetValue(AValue: T);
begin
  FValue := AValue;
  FHasValue := True;
end;

procedure TNullable<T>.Clear;
begin
  FHasValue := False;
end;

class operator TNullable<T>.Implicit(A: T): TNullable<T>;
begin
  Result.Value := A;
end;

class operator TNullable<T>.Implicit(A: Pointer): TNullable<T>;
begin
  if A = nil then Result.Clear
  else raise Exception.Create('Pointer value not allowed');
end;

end.
单元可为空;
{$modedelphi}{$H+}
接口
使用
类,sysutil;
类型
TNullable=记录
私有的
FHasValue:布尔值;
f值:T;
函数GetValue:T;
程序设定值(AValue:T);
公众的
程序清晰;
属性HasValue:布尔读取FHasValue;
属性值:T读取GetValue写入SetValue;
类运算符隐式(A:T):T可使用;
类运算符隐式(A:指针):TNullable;
结束;
实施
{TNullable}
函数TNullable.GetValue:T;
开始
如果fhas值那么
结果:=FValue
其他的
引发异常。创建('变量没有值');
结束;
程序TNullable.SetValue(AValue:T);
开始
f值:=有效值;
FHasValue:=真;
结束;
程序可使用。清晰;
开始
FHasValue:=假;
结束;
类运算符TNullable.Implicit(A:T):TNullable;
开始
结果.值:=A;
结束;
类运算符TNullable.Implicit(A:指针):TNullable;
开始
如果A=nil,则结果为。清除
否则引发异常。创建('Pointer value not allowed');
结束;
结束。

您可以在delphi中使用指针。请使用可为空的类型。例如,Spring有一个。无论如何,这永远不会是VCL类型,它是RTL。试试网络搜索。你知道@Wimmel使用指针意味着什么吗?谷歌搜索delphi nullable时有很多点击,其中也包含代码。他们几乎都以同样的方式工作(有记录)
unit Nullable;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils;

type
  TNullable<T> = record
  private
    FHasValue: Boolean;
    FValue: T;
    function GetValue: T;
    procedure SetValue(AValue: T);
  public
    procedure Clear;
    property HasValue: Boolean read FHasValue;
    property Value: T read GetValue write SetValue;
    class operator Implicit(A: T): TNullable<T>;
    class operator Implicit(A: Pointer): TNullable<T>;
  end;

implementation

{ TNullable }

function TNullable<T>.GetValue: T;
begin
  if FHasValue then
     Result := FValue
  else
    raise Exception.Create('Variable has no value');
end;

procedure TNullable<T>.SetValue(AValue: T);
begin
  FValue := AValue;
  FHasValue := True;
end;

procedure TNullable<T>.Clear;
begin
  FHasValue := False;
end;

class operator TNullable<T>.Implicit(A: T): TNullable<T>;
begin
  Result.Value := A;
end;

class operator TNullable<T>.Implicit(A: Pointer): TNullable<T>;
begin
  if A = nil then Result.Clear
  else raise Exception.Create('Pointer value not allowed');
end;

end.