2PTTechnology

The platform that enables you to build rich, interactive communities
Welcome to 2PTTechnology Sign in | Join | Help
in Search

How to utilize inline if statements in C#

Last post 10-16-2007, 2:08 AM by Anonymous. 1 replies.
Sort Posts: Previous Next
  •  03-22-2007, 11:19 AM 35

    How to utilize inline if statements in C#

    Here is a simple example of how to utilize an inline if statement in your C# code. One of the benefits in using inline if statements is less declaration of variables and less code and applying a condition in one line of code.

     

    Here are two examples of the same if statement. The second one is using inline. In this example we are checking if variable “a” is equal to 5, if so assign “five” to variable “b” if not than assign “zero” to variable “b”.

     

    Variable = (condition ? true value : false value )

     

    Original way:

    int a = 5;

                string b = string.Empty;

     

                if (a == 5)

                {

                    b = "five";

                }

                else

                {

                    b = "zero";

                }

     

    Inline way:

                b = (a == 5 ? "five" : "zero");

  •  10-16-2007, 2:08 AM 59 in reply to 35

    Re: How to utilize inline if statements in C#

    Very simple and nice way of explaining!

    Thank you!

    Lars, Denmark

    admin:

    Here is a simple example of how to utilize an inline if statement in your C# code. One of the benefits in using inline if statements is less declaration of variables and less code and applying a condition in one line of code.

     

    Here are two examples of the same if statement. The second one is using inline. In this example we are checking if variable “a” is equal to 5, if so assign “five” to variable “b” if not than assign “zero” to variable “b”.

     

    Variable = (condition ? true value : false value )

     

    Original way:

    int a = 5;

                string b = string.Empty;

     

                if (a == 5)

                {

                    b = "five";

                }

                else

                {

                    b = "zero";

                }

     

    Inline way:

                b = (a == 5 ? "five" : "zero");

View as RSS news feed in XML
Powered by Community Server (Personal Edition), by Telligent Systems