我通常使用 C# 編程,但也使用舊語言 Delphi。在 Delphi 中,據我所知,一個屬性使用了很多這樣的代碼:
private
FOPPORTUNITY_NR : string;
procedure SetOPPORTUNITY_NR(const aOPPORTUNITY_NR: string);
function GetOPPORTUNITY_NR: string;
public
property OPPORTUNITY_NR: string read GetOPPORTUNITY_NR write SetOPPORTUNITY_NR;
implementation
procedure TTypeName.SetOPPORTUNITY_NR(const aOPPORTUNITY_NR: string);
begin
if (SpecialStrUtils_13.IsBlankStr(aOPPORTUNITY_NR)) then
raise Exception.CreateFmt('OPPORTUNITY_NR cannot be empty');
FOPPORTUNITY_NR := aOPPORTUNITY_NR;
end;
function TTypeName.GetOPPORTUNITY_NR: string;
begin
if (SpecialStrUtils_13.IsBlankStr(FOPPORTUNITY_NR)) then
raise Exception.CreateFmt('OPPORTUNITY NUMBER not set');
Result := FOPPORTUNITY_NR;
end;
在 C# 中,我可以像這樣撰寫上面的代碼:
private string? _opportunityNr = null;
public string OpportunityNr
{
get => _opportunityNr ?? throw new Exception("Opportunitynr not set");
set => _opportunityNr = String.IsNullOrEmpty(value) ? throw new Exception("Opportunitynr can not be empty") : value;
}
Delphi 中是否有像 C# 中更短的符號?
uj5u.com熱心網友回復:
Delphi 中是否有像 C# 中更短的符號?
不。
uj5u.com熱心網友回復:
您不需要使用方法:
property OPPORTUNITY_NR: string read FOPPORTUNITY_NR write FOPPORTUNITY_NR;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/505163.html