重複する質問
.Net 2.0のc#でこれを行うことはできますか?
public void myMethod(string astring, int? anint)
{
//some code in which I may have an int to work with
//or I may not...
}
そうでない場合、私ができる同様のことはありますか?
重複する質問
.Net 2.0のc#でこれを行うことはできますか?
public void myMethod(string astring, int? anint)
{
//some code in which I may have an int to work with
//or I may not...
}
そうでない場合、私ができる同様のことはありますか?
はい、シェブロンを意図的に追加し、実際に次のことを意味していると仮定します。
public void myMethod(string astring, int? anint)
anint
これでHasValue
プロパティが作成されます。
何を達成したいかによります。anint
パラメータを削除できるようにする場合は、オーバーロードを作成する必要があります。
public void myMethod(string astring, int anint)
{
}
public void myMethod(string astring)
{
myMethod(astring, 0); // or some other default value for anint
}
これで、次のことができます。
myMethod("boo"); // equivalent to myMethod("boo", 0);
myMethod("boo", 12);
null許容整数を渡したい場合は、他の回答を参照してください。;)
C#2.0では、次のことができます。
public void myMethod(string astring, int? anint)
{
//some code in which I may have an int to work with
//or I may not...
}
そして、次のようなメソッドを呼び出します
myMethod("Hello", 3);
myMethod("Hello", null);