これは特に、書き直したいC#プロパティを参照しています。元のプロパティの1のnull伝播演算子のパフォーマンスとコンパイル
例:
public double? PartQuantity => MaintenanceRequestPart?.ReportedQuantity;
注1:MaintenanceRequestPart
がnull
注2することができ、これはに変更されますどのような
public double? PartQuantity
{
get
{
if(MaintenanceRequestPart != null)
return MaintenanceRequestPart.ReportedQuantity.HasValue
? (double?)MaintenanceRequestPart.ReportedQuantity.Value
: null;
return null;
}
}
:MaintenanceRequestPart.ReportedQuantity
はヌルブルブルです
これは、いくつかの操作/分岐/オーバーヘッドを保存するか追加しますか?私は何が好きですか?オペレータは実際に中間言語に変換されると、実際には背後に翻訳されます。
を失うことなく、このリファクタリングを行うことができますReportedQuantity'がすでにNULL可能ダブルで 'ので、元のコードでは、なぜあなたは、単に(MaintenanceRequestPart = nullで!)場合は'行っていませんreturn MaintenanceRequestPart.ReportedQuantity; ' – dbc
また、https://ericlippert.com/2012/12/17/performance-rant/も参照してください。自分でテストしてみませんか? – dbc
ILがそのコードに対して何をするかを知りたい場合は、ILをコンパイルして調べます。あなたは私たちがあなたのためにそれをコンパイルする必要はありません。 – Servy