C# Difference between using Missing.Value and just leaving out the optional parameter from method call?

▼魔方 西西 提交于 2019-12-11 09:44:56

问题


So, after extensively searching, I found a lot of information about what the Missing.Value does passing the default value to an optional parameter.

But somehow not a single mention about why is it more useful, than just leaving out the optional parameter alltogether in which case, it also gets it's default value.

Came across it in a Ms Office interop code sample, but tried leaving it out, and works all the same, just as excpected, so, what gives?

E.g. in :

oWB = oXL.Workbooks.Add( Missing.Value );

vs

oWB = oExcel.Workbooks.Add();

回答1:


Just to clarify benifit of Optional/Named parameters with different example because oWB = oXL.Workbooks.Add( Missing.Value ); is already shared.

Missing.Value

(From Remarks) Use this instance of the Missing class to represent missing values, for example, when you invoke methods that have default parameter values.

Example from Missing Class

    public class MissingClass
    {
        public static void MethodWithDefault(int value = 33)
        {
            Console.WriteLine("value = {0}", value);
        } 
    }

public class Example
{
   public static void Main()
   {
      // Invoke without reflection
      MissingClass.MethodWithDefault();

      // Invoke by using reflection.
      Type t = typeof(MissingClass);
      MethodInfo mi = t.GetMethod("MethodWithDefault");
      mi.Invoke(null, new object[] { Missing.Value });
   }
}
// The example displays the following output:
//       value = 33  
//       value = 33  

Optional Parameter, See following example ,if caller of this method(GetBillingInfo) does not supply values for those parameters then the default value provided to the parameters will be used

You will be able to call your method by 4 ways

GetBillingInfo();//No parametes (kValue,quoteID)
GetBillingInfo("kvalue"); // KValue
GetBillingInfo("KValue" , 20); // kvalue + quoteID

You can call it with paramName (Named Parameters)

GetBillingInfo(quoteID: 20) //quoteID

Read Named and Optional Arguments (C# Programming Guide)




回答2:


Your confusion is well-founded. In this day and age, there is no real use for Missing.Value. But in the dark ages before C# 4.0 (2010, I think?), optional parameters were not yet a feature of the C# language.

This means that if you wanted to work with COM Interop interfaces that relied on optional parameters and exposed a large number of such parameters - with Office interop being a prime example, you either had to pass Missing.Value for each argument you didn't want (over 15, for some calls!) or use VB.NET to write a wrapper around the COM calls. VB.NET was explicitly designed to support this sort of COM interop, as it was a common use for VB before it.

But today? Just drop the optional params.

More info can be found in this old answer to a related question:

  • https://softwareengineering.stackexchange.com/questions/185186/com-interop-support-which-is-better-c-or-vb/185191#185191


来源:https://stackoverflow.com/questions/48249700/c-sharp-difference-between-using-missing-value-and-just-leaving-out-the-optional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!