我正在将 VB 代码转换为 c#:
Private Function SoundsLike(ByVal pWord As String,
Optional ByRef pAccuracy As Byte = 6) As String.
但我得到了不同类型的参数。让我知道如何用 C# 编写代码。
我正在将 VB 代码转换为 c#:
Private Function SoundsLike(ByVal pWord As String,
Optional ByRef pAccuracy As Byte = 6) As String.
但我得到了不同类型的参数。让我知道如何用 C# 编写代码。
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
代码如下:
private string SoundsLike(string pWord, byte pAccuracy = 6);
需要 C# 4.0,因为包含可选参数。对于早期版本,同样可以通过重载来实现。
利用
private string SoundsLike(string pWord, byte pAccuracy = 6)
要不就
private string SoundsLike(string pWord, out byte pAccuracy)
Private
是可选的。如果没有给出修饰符,默认为Private
void abc(){}
和
private void abc() {}
与变量相同。