如何VB.NET可选参数工作“引擎盖下”?他们是符合CLS? [英] How Do VB.NET Optional Parameters work 'Under the hood'? Are they CLS-Compliant?

查看:205
本文介绍了如何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, _
    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#.

这篇关于如何VB.NET可选参数工作“引擎盖下”?他们是符合CLS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆