Click here to Skip to main content
15,923,168 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,
I'm a c# beginner and I don't know what's '?' and its structure in this code:
C#
public bool IsLeapYear(int year)
{
    return year % 4 == 0 ? true : false;
}
Posted
Updated 15-Nov-10 8:54am
v3

This is an operator and means the same as:
C#
public bool IsLeapYear(int year)
{
    if (year % 4 == 0)
        return true;
    else
        return false;
}

or, simpler:
C#
public bool IsLeapYear(int year)
{
    return year % 4 == 0;
}

Nonetheless I would recommend you to use:
C#
System.DateTime.IsLeapYear(year);

because your code is incomplete for correct Leap Year. Below is the complete code:
C#
public bool IsLeapYear(int year)
{
    if (year % 400 == 0)
        return true;
    else if (year % 100 == 0)
        return false;
    else if (year % 4 == 0)
        return true;
    else
        return false;
}

or, simpler:
C#
public bool IsLeapYear(int year)
{
    if (year % 400 == 0)
        return true;
    if (year % 100 == 0)
        return false;
    if (year % 4 == 0)
        return true;
    return false;
}
 
Share this answer
 
v4
Comments
Johnny J. 15-Nov-10 7:23am    
Good answer
thatraja 15-Nov-10 7:36am    
Hi JF, I was added just the reason with code block for your statement. :-)
Hi,

? is called conditional operator. more about that you can find on msdn:
http://msdn.microsoft.com/en-us/library/ty67wk28(VS.80).aspx[^]

and

http://msdn.microsoft.com/en-us/library/6a71f45d[^]

Regards
Robert
 
Share this answer
 
v2
Hi this is same as ternary operator in C
It just executes the Conditional expression to the left of the ? operator. If the result satisfies (means true) it execute the expression immediate next to ? operator, otherwise it execute the expression after the : operator

if suppose year is 1996

return 1996%4==0 ? true : False


in this the compiler first execute the conditions to the left of ? operator
then
return 0==0 ? true : False

condition true so it start execute statement immediately next to ? here its return true, otherwise in this example say year =1998 the calculated value is not equal to zero It ignores the Statement immediately next to ? operator and execute the statement after the : operator which is False
hence it returns false

lets say a=10 b=15

public int void operate()
{
 c=(a>b)? a-b:a+b;
}
this is same like

if (a>b)
c=a-b;
else 
c=a+b;

for this condition c=25 because (a>b) is false. you can use this for simple conditional checking.
 
Share this answer
 
v2
Although some answers let you believe that the operator is only compatible with giving boolean results (which would actually not be very useful at all) it gives you a short notation for a simple if/then/else construction.

It would be no help if you substituted this:
return value > 0;


by this:
return value > 0 ? true : false;


It would mean that your code would get longer instead of shorter. The '?' operator is meant to return any type based on the value of a boolean.

For example:
return value > 0 ? "the value is greater than zero", "the value is equal or lower  than zero";
return value > 0 ? 1 : -1;


But using it holds minor and major risks that you need to be aware of. This is because both expressions are evaluated.

This example always creates one object that will be unused:
return compressed ? new JpegImage() ? BitmapImage();


This example goes wrong if value is zero because size / value is evaluated anyway:
return value > 0 ? size / value : size;


Now just another example that would be a major bug in your software. Would the record exists, even if the action is t update the record?
return action == update ? UpdateRecord(record) : DeleteRecord(record);


Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900