如何VB.NET可选参数工作“引擎盖下”?他们是符合CLS?引擎盖、可选、参数、工作

由网友(流年划破容颜╮)分享简介:比方说,我们有以下的方法声明:Let's say we have the following method declaration:Public Function MyMethod(ByVal param1 As Integer, _Optional ByVal param2 As Integer = 0, _Op...

比方说,我们有以下的方法声明:

Let's say we have the following method declaration:

Public Function MyMethod(ByVal param1 As Integer, _
    Optional ByVal param2 As Integer = 0, _
    Optional ByVal param3 As Integer = 1) As Integer

    Return param1 + param2 + param3
End Function

如何VB.NET使可选参数的CLR的范围内开展工作?可选参数符合CLS?

How does VB.NET make the optional parameters work within the confines of the CLR? Are optional parameters CLS-Compliant?

推荐答案

有趣的是,这是反编译的C#code,通过反射得到。

Interestingly, this is the decompiled C# code, obtained via reflector.

public int MyMethod(int param1, 
                   [Optional, DefaultParameterValue(0)] int param2, 
                   [Optional, DefaultParameterValue(1)] int param3)
{
    return ((param1 + param2) + param3);
}

注意选配和DefaultParameterValue属性。尝试把它们在C#中的方法。你会发现,你仍然需要将值传递给该方法。在VB code然而,它变成默认的!话虽这么说,我个人从来不使用默认值,即使在VB code。这感觉就像一个黑客。方法重载的伎俩我。

Notice the Optional and DefaultParameterValue attributes. Try putting them in C# methods. You will find that you are still required to pass values to the method. In VB code however, its turned into Default! That being said, I personally have never use Default even in VB code. It feels like a hack. Method overloading does the trick for me.

默认确实有助于不过,当使用Excel互操作,这是一个痛苦的屁股直使用的开箱即用在C#中。处理

Default does help though, when dealing with the Excel Interop, which is a pain in the ass to use straight out of the box in C#.

阅读全文

相关推荐

最新文章