Search code examples
c#vb.netrpauipath

VB to C# conversion error (assign and if statements)


what is the proper way to convert

if((year mod 4=0 and year mod 100<>0) or (year mod 400=0), “Leap Year”, “Not a Leap Year”)

To C#

I was able to successfully convert the first part if ((year % 4 == 0 & year % 100 != 0) | (year % 400 == 0)) but when I add the messages, I get an error.

Any help would be greatly appreciated.


Solution

  • The equivalent of that VB If operator is the C# ternary operator (?:), i.e.

    If(x, y, z)
    

    is equivalent to:

    x ? y : z;
    

    For the record, there's another If operator like this:

    If(x, y)
    

    which evaluates to x if x is not null, otherwise it evaluates to y. The C# equivalent is called the null coalescing operator (??):

    x ?? y;