Check inside the method if any additional argument was passed

How to check if an extra argument has been passed to a method?

public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { if (optionalint was passed) return; } 

Another approach is to use Nullable<T>.HasValue ( MSDN definitions, MSDN examples ):

 int default_optionalint = 0; public void ExampleMethod(int required, int? optionalint, string optionalstr = "default string") { int _optionalint = optionalint ?? default_optionalint; } 
+8
c # nullable optional-parameters argument-passing
source share
8 answers

Well, arguments are always passed on. The default parameter values ​​simply ensure that the user does not need to explicitly specify them when calling the function.

When the compiler sees a call like this:

 ExampleMethod(1); 

He quietly converts it to:

 ExampleMethod(1, "default string", 10); 

Therefore, it is technically impossible to determine whether an argument was passed at run time. The closest you can get is:

 if (optionalstr == "default string") return; 

But this will behave the same if the user so explicitly calls it:

 ExampleMethod(1, "default string"); 

An alternative, if you really want to have a different behavior depending on whether a parameter is provided, is to get rid of the default parameters and use overloads, for example:

 public void ExampleMethod(int required) { // optionalstr and optionalint not provided } public void ExampleMethod(int required, string optionalstr) { // optionalint not provided } public void ExampleMethod(int required, string optionalstr, int optionalint) { // all parameters provided } 
+12
source share

You cannot, in principle. The IL generated for these calls is exactly the same:

 ExampleMethod(10); ExampleMethod(10, "default string"); ExampleMethod(10, "default string", 10); 

The default execution is performed on the call site by the compiler.

If you really want both of these calls to be valid, but distinguishable, you can simply use overload:

 // optionalint removed for simplicity - you'd need four overloads rather than two public void ExampleMethod(int required) { ExampleMethodImpl(required, "default string", false); } public void ExampleMethod(int required, string optionalstr) { ExampleMethodImpl(required, optionalstr, true); } private void ExampleMethodImpl(int required, int optionalstr, bool optionalPassed) { // Now optionalPassed will be true when it been passed by the caller, // and false when we went via the "int-only" overload } 
+6
source share

You cannot do this in C #.

However, you can overload the function to accept different arguments. This, by the way, is the only approach you can use in Java so that you are in good company if you accept it.

+4
source share

You cannot verify this because a method with optional parameters is a regular method with all parameters, including those that have default values. Thus, your method will be compiled into:

 public void ExampleMethod(int required, string optionalstr, int optionalint) { } 

The default values ​​are inserted by the compiler at the dial peer. If you write

 ExampleMethod(42); 

Then the compiler will generate a call

 ExampleMethod(42, "default string", 10); 

You can compare if the value of optionalstr or optionalint has a value equal to the default value, but you cannot tell if it was provided by the compiler or developer.

+3
source share

You cannot, so you need to find another way to check the "optional" parameter. You can pass null if the parameter is not used, then check

 if (optionalstr != null) { // do something } 

You can also overload the method using one of the optional parameters and one that does not accept optional parameters. In addition, you can do this so that the method without additional parameters is passed in NULL values ​​to one of the overloaded methods.

 public void ExampleMethod(int required) { ExampleMethod(required, null, 0); } public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { } 
+2
source share

you cannot check directly, but you can check it by default. eg:

 public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { if (optionalint == 10) return; } 

or

 public void ExampleMethod(int required, string optionalstr = "default string", int? optionalint) { if (required.HasValue==false) return; } 

Approach 2:

You can also use override methods:

 public void ExampleMethod(int required, string optionalstr = "default string") { //When this method called, means optionalint was NOT passed } public void ExampleMethod(int required, string optionalstr = "default string", int optionalint) { //When this method called, means optionalint was passed } 
+1
source share

Another approach is to use Nullable<T>.HasValue ( MSDN definitions , MSDN examples ):

 int default_optionalint = 0; public void ExampleMethod(int required, int? optionalint, string optionalstr = "default string") { int _optionalint = optionalint ?? default_optionalint; } 
0
source share

To determine if an argument has been passed to an optional parameter

  • Define an extremely unlikely value as the default value for the parameter.
  • If the optional parameter is a reference type, such as String , you can use null as the default if this is not the expected value for the argument.
  • In the procedure code, compare the parameter with the default value and perform the appropriate actions.

https://msdn.microsoft.com/en-us/library/849zff9h(v=vs.100).aspx

-one
source share

All Articles