什么是正确的转换方法
if((year mod 4=0 and year mod 100<>0) or (year mod 400=0), “Leap Year”, “Not a Leap Year”)
Run Code Online (Sandbox Code Playgroud)
到 C#
我能够成功转换第一部分, if ((year % 4 == 0 & year % 100 != 0) | (year % 400 == 0))但是当我添加消息时,出现错误。
任何帮助将不胜感激。
VBIf运算符的等价物是 C#三元运算符( ?:),即
If(x, y, z)
Run Code Online (Sandbox Code Playgroud)
相当于:
x ? y : z;
Run Code Online (Sandbox Code Playgroud)
为了记录,还有另一个If像这样的运算符:
If(x, y)
Run Code Online (Sandbox Code Playgroud)
其计算结果为xifx不是null,否则计算结果为y。C# 等效项称为空合并运算符( ??):
x ?? y;
Run Code Online (Sandbox Code Playgroud)
原始 VB 代码应该使用DateTime.IsLeapYear(Int32) Method,以便在 C# 中它会变成:
DateTime.IsLeapYear(year) ? "Leap Year" : "Not a Leap Year";
Run Code Online (Sandbox Code Playgroud)