C++ 空指针参数作为 C# 中的可选参数替代
C++ null pointer argument as optional argument alternative in C#
我需要在 C# 中 translate/rewrite 一些 C++ 代码。很多方法,写C++代码的人在原型里都做过这样的事情,
float method(float a, float b, int *x = NULL);
然后在方法中是这样的,
float method(float a, float b, int *x) {
float somethingElse = 0;
int y = 0;
//something happens here
//then some arithmetic operation happens to y here
if (x != NULL) *x = y;
return somethingElse;
}
我已经确认 x
是该方法的可选参数,但现在我在用 C# 重写它时遇到了问题。除非我使用指针并进入不安全模式,否则我不太确定该怎么做,因为 int
不能是 null
。
我试过这样的东西,
public class Test
{
public static int test(ref int? n)
{
int x = 10;
n = 5;
if (n != null) {
Console.WriteLine("not null");
n = x;
return 0;
}
Console.WriteLine("is null");
return 1;
}
public static void Main()
{
int? i = null;
//int j = 100;
test(ref i);
//test(ref j);
Console.WriteLine(i);
}
}
如果我在 main()
方法中取消注释带有变量 j
的行,代码不会编译并提示类型 int
与类型 [=19] 不匹配=].但无论哪种方式,这些方法将在以后使用并且 int
将传递给它们,所以我不太热衷于使用 int?
来保持兼容性。
我研究了 C# 中的可选参数,但这仍然不意味着我可以使用 null
作为 int
的默认值,而且我不知道这个变量赢得了哪些值'遇到.
我也研究了 ??
空合并运算符,但这似乎与我正在尝试做的相反。
我能得到一些关于我应该做什么的建议吗?
提前致谢。
j
也应声明为可为空,以匹配参数类型。 i
和 j
然后都应像它们一样传递给接收可为 null 的 int 参数的函数。
此外,您还在函数内部为 n
赋值,因此无论您尝试什么,您的代码总是会遇到 not null
情况。
这应该有效:
public static int test(int? n) // without the keyword ref
{
int x = 10;
//n = 5; // Why was that??
if (n != null)
{
Console.WriteLine("not null");
n = x;
return 0;
}
Console.WriteLine("is null");
return 1;
}
static void Main(string[] args)
{
int? i = null; // nullable int
int? j = 100; // nullable to match the parameter type
test(i);
test(j);
Console.WriteLine(i);
}
在我看来你想要一个可选的 out
参数。
我会在 C# 中使用重写。
public static float method(float a, float b, out int x){
//Implementation
}
public static float method(float a, float b){
//Helper
int x;
return method(a, b, out x);
}
我需要在 C# 中 translate/rewrite 一些 C++ 代码。很多方法,写C++代码的人在原型里都做过这样的事情,
float method(float a, float b, int *x = NULL);
然后在方法中是这样的,
float method(float a, float b, int *x) {
float somethingElse = 0;
int y = 0;
//something happens here
//then some arithmetic operation happens to y here
if (x != NULL) *x = y;
return somethingElse;
}
我已经确认 x
是该方法的可选参数,但现在我在用 C# 重写它时遇到了问题。除非我使用指针并进入不安全模式,否则我不太确定该怎么做,因为 int
不能是 null
。
我试过这样的东西,
public class Test
{
public static int test(ref int? n)
{
int x = 10;
n = 5;
if (n != null) {
Console.WriteLine("not null");
n = x;
return 0;
}
Console.WriteLine("is null");
return 1;
}
public static void Main()
{
int? i = null;
//int j = 100;
test(ref i);
//test(ref j);
Console.WriteLine(i);
}
}
如果我在 main()
方法中取消注释带有变量 j
的行,代码不会编译并提示类型 int
与类型 [=19] 不匹配=].但无论哪种方式,这些方法将在以后使用并且 int
将传递给它们,所以我不太热衷于使用 int?
来保持兼容性。
我研究了 C# 中的可选参数,但这仍然不意味着我可以使用 null
作为 int
的默认值,而且我不知道这个变量赢得了哪些值'遇到.
我也研究了 ??
空合并运算符,但这似乎与我正在尝试做的相反。
我能得到一些关于我应该做什么的建议吗?
提前致谢。
j
也应声明为可为空,以匹配参数类型。 i
和 j
然后都应像它们一样传递给接收可为 null 的 int 参数的函数。
此外,您还在函数内部为 n
赋值,因此无论您尝试什么,您的代码总是会遇到 not null
情况。
这应该有效:
public static int test(int? n) // without the keyword ref
{
int x = 10;
//n = 5; // Why was that??
if (n != null)
{
Console.WriteLine("not null");
n = x;
return 0;
}
Console.WriteLine("is null");
return 1;
}
static void Main(string[] args)
{
int? i = null; // nullable int
int? j = 100; // nullable to match the parameter type
test(i);
test(j);
Console.WriteLine(i);
}
在我看来你想要一个可选的 out
参数。
我会在 C# 中使用重写。
public static float method(float a, float b, out int x){
//Implementation
}
public static float method(float a, float b){
//Helper
int x;
return method(a, b, out x);
}