Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OptionalAttribute parameter's default value?

MSDN's VS2010 Named and Optional Arguments (C# Programming Guide) tells us about optional parameters in C#, showing code like I'd expect:

public void ExampleMethod(int required, 
    string optionalstr = "default string", 
    int optionalint = 10)

Ok, but it also says:

You can also declare optional parameters by using the .NET OptionalAttribute class. OptionalAttribute parameters do not require a default value.

I read MSDN's OptionalAttribute page, and done searches online (which shows lots of people claiming OptionalAttribute parameters can't be consumed by C# -- I'm guessing these comments were made before C# 4?), but I can't find the answer to two questions:

If I use OptionalAttribute to define a C# parameter as optional:

  1. what value will be used if I call that method and don't specify that parameter's value?
  2. will that value be evaluated at compile time or runtime?
like image 228
lance Avatar asked Aug 05 '10 15:08

lance


People also ask

What is default parameters in C#?

By default, all parameters of a method are required. A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments. The optional parameter contains a default value in function definition.

How do you pass optional parameters in C#?

By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.

Can out parameter be optional C#?

also C# and . NET framework hast many many data structures that are very flexible like List and Array and you can use them as an output parameter or as return type so there is no need to implement a way to have optional output parameters.

What is optional parameter?

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported.


1 Answers

The rules are this:

  • For parameters of type object, Type.Missing is passed.
  • For other reference types, null is passed.
  • For value types, the default of the value type is passed.
    • For Nullable<T> this means that you will get a Nullable<T> instance which is equal to null (the HasValue property will return false)

Note that in the case of everything except parameters of type object, it's the equivalent of default(T).

I was a little surprised, as the C# 4.0 specification didn't indicate what the outcome would be, and I'd expect it to be there.

Also (as indicated by Scott Rippey in the comments), this is evaluated at compile-time, this is not a run-time operation, meaning that if you have calls to this method in other assemblies which are already deployed, and you change the optional value, the default passed to the method will not change unless you compile everything that makes the call against the method in the assembly.

like image 177
casperOne Avatar answered Sep 28 '22 23:09

casperOne