Duplicate Question
Passing null arguments to C# methods
Can I do this in c# for .Net 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...
}
If not, is there something similar I can do?
Depends on what you want to achieve. If you want to be able to drop the
anint
parameter, you have to create an overload:You can now do:
If you want to pass a nullable int, well, see the other answers. ;)
In C# 2.0 you can do;
And call the method like
Yes, assuming you added the chevrons deliberately and you really meant:
anint
will now have aHasValue
property.