Elegant check for null and exit in C#

Posted by aip.cd.aish on Stack Overflow See other posts from Stack Overflow or by aip.cd.aish
Published on 2010-03-14T03:50:19Z Indexed on 2010/03/14 3:55 UTC
Read the original article Hit count: 238

Filed under:
|
|
|
|

What is an elegant way of writing this?

if (lastSelection != null)
{
    lastSelection.changeColor();
}
else
{
    MessageBox.Show("No Selection Made");
    return;
}

changeColor() is a void function and the function that is running the above code is a void function as well.

© Stack Overflow or respective owner

Related posts about c#

Related posts about coding-style