I am using a method for doing some action, i want the method to be written only once by using Optional Parameters in C#, other than Method Overloading is there any?
The optional declaration-specifiers and mandatory declarator together specify the function's return type and name. The declarator is a combination of the identifier that names the function and the parentheses following the function name.
Using the Logical OR operator ('||') In this method, the optional parameter is "Logically ORed" with the default value within the body of the function. In the example below, if the value of b is undefined, 2 is passed instead.
To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.
New to visual studio 2010
named and optional arguments
for example
public void ExampleMethod(int required, string optionalstr = "default string",
int optionalint = 10)
{
}
Have a look at following code
Library to use
using System.Runtime.InteropServices;
Function declaration
private void SampleFunction([Optional]string optionalVar, string strVar)
{
}
And while giving call to function you can do like this
SampleFunction(optionalVar: "someValue","otherValue");
OR
SampleFunction("otherValue");
Reply if it helps.!:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With