読み取り専用フィールドに値を供給する2つのコンストラクターがあります。
public class Sample
{
public Sample(string theIntAsString)
{
int i = int.Parse(theIntAsString);
_intField = i;
}
public Sample(int theInt) => _intField = theInt;
public int IntProperty => _intField;
private readonly int _intField;
}
1つのコンストラクターは値を直接受け取り、もう1つのコンストラクターは何らかの計算を行って値を取得してから、フィールドを設定します。
これがキャッチです:
何か案は?
このような:
public Sample(string str) : this(int.Parse(str)) { }
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加