【问题标题】:VB to C# Code TranslatorVB 到 C# 代码翻译器
【发布时间】:2012-05-17 07:19:17
【问题描述】:

我正在将 VB 代码转换为 c#:

Private Function SoundsLike(ByVal pWord As String, 
             Optional ByRef pAccuracy As Byte = 6) As String.

但是我得到了不同类型的参数。让我知道如何用 C# 编写代码。

【问题讨论】:

标签: c# vb.net vb.net-to-c#


【解决方案1】:

VB.Net

Private Function SoundsLike(ByVal pWord As String, Optional ByRef pAccuracy As Byte = 6) As String

C#

private string SoundsLike(string pWord, byte pAccuracy = 6)
{
}

private string SoundsLike(string pWord, out byte pAccuracy)
{
}

注意outref 不能有默认值

仅供参考:“out 关键字导致参数通过引用传递。这类似于 ref 关键字,只是 ref 要求在传递之前初始化变量。” 参考:http://geekswithblogs.net/ftom/archive/2008/09/10/c-and-the-difference-between-out-and-ref.aspx

【讨论】:

    【解决方案2】:

    代码如下:

    private string SoundsLike(string pWord, byte pAccuracy = 6);
    

    需要 C# 4.0,因为包含可选参数。对于早期版本,同样可以通过重载实现。

    【讨论】:

    • 第一次添加答案时不存在。
    • @NikhilAgrawal:按 ref 传递字节对我来说毫无意义。
    • @abatishchev 高中年鉴的照片姿势让我死了,伙计。干得好。
    • @Yatrix:很棒的评论,伙计。等了好几年了:D
    • @abatishchev 有些人就是不了解真正的天才,伙计。我愿意。 =)
    【解决方案3】:

    使用

    private string SoundsLike(string pWord, byte pAccuracy = 6)
    

    或者只是

     private string SoundsLike(string pWord, out byte pAccuracy)
    

    Private 是可选的。如果没有给出修饰符,默认为Private

    void abc(){}
    

    相同
    private void abc() {}
    

    与变量相同。

    【讨论】:

    • ref 不能有默认值
    • @JasonJong:更正:谢谢
    • 另外,永远不要关闭修饰符。这是不好的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多