I have function with parameters as given below.
public void myfunction(int a, string b){}
Can we call this function by passing only first parameter and second parameter is optional(nullable string)?
myfunction(5);
If yes then how.
Actually, your question is a little bit confusing because by the term nullable
in the title, meantime you are looking for a method having default parameters/Optional parameters. which can be written like the following:
public void myfunction(int a, string b = ""){}
So that you can call like this myfunction(5);
or like myfunction(5,null);
or even this: myfunction(5,"Something");
just overload the function as follows:
public void myfunction(int a, string b)
{
//do stuff
}
public void myfunction(int a)
{
myfunction(a,string.empty);
}
then call
myFuntion(5);
Note: It's best practice to use string.empty instead of null so have shown that in my example.
string?
does not compile.string
is already nullable and cannot be used as a type inNullable<T>
.