-1

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

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

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

4

3 回答 3

5

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)
{
}

请注意,out并且ref不能有默认值

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

于 2012-05-17T07:23:15.323 回答
3

代码如下:

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

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

于 2012-05-17T07:21:24.310 回答
1

利用

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

要不就

 private string SoundsLike(string pWord, out byte pAccuracy)

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

void abc(){}

private void abc() {}

与变量相同。

于 2012-05-17T07:21:29.833 回答