Background
In this article am trying solve one more Performance issue IF condition and Conditional Expression. In office today debut on this about which is best so I decided make a simple program and got the Result which best.

Performance
Every human think about speed means performance is want in every where like Internet speed, LP speed and so on. So this article based on Performance issue.
IF Condition
- string HoldValue=string.Empty;
- If(HoldValue==string.Empty)
- {
- do stuff here ////// if condition is True
- Holdvalue="Test Value";
- }
- else
- {
- do stuff here///// if Condition is False
- HoldValue="";
- }
Conditional Expression
- string HoldValue=string.Empty;
- HoldValue=HoldValue==string.string.Empty ? "Test Value" : "";
Expression of Conditional Expression.
- Variable=conditional Value==Condition ? "Assign this if Condition is true: Assign this if Condition is False;
Compare performance between If and Conditional Expression
We are used the native code for performance checked like a For Loop. I have used one single console application.
- static void Main(string[] args)
- {
- Program p = new Program();
- p.SimpleIfElse();
- p.ConditionalExpression();
- }
Method SimpleIfElse
- public void SimpleIfElse()
- {
- Console.WriteLine("Don't worry be Relex your System is Ok ☺");
- Console.WriteLine("SimpleIfElse Call here");
- string startingTime = DateTime.Now.ToLongTimeString();
- for (int i = 0; i < 1000000000; i++)
- {
- if (HoldTempValue == string.Empty)
- {
- HoldTempValue = "TestingValue";
- }
- HoldTempValue = string.Empty;
- }
- string endtime = DateTime.Now.ToLongTimeString();
- TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));
- Console.WriteLine(t1);
- }
Start Time set here.
- string startingTime = DateTime.Now.ToLongTimeString();
End time set here.
- string endtime = DateTime.Now.ToLongTimeString();
- TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));
Method Conditional Expression
- public void ConditionalExpression()
- {
- Console.WriteLine("ConditionalExpression call here");
- string endtime = DateTime.Now.ToLongTimeString();
- string startingTime = DateTime.Now.ToLongTimeString();
- for (int i = 0; i < 1000000000; i++)
- {
- HoldTempValue = HoldTempValue == string.Empty ? "TestingValue" : "";
- HoldTempValue = string.Empty;
- }
- endtime = DateTime.Now.ToLongTimeString();
- TimeSpan t1 = DateTime.Parse(endtime).Subtract(DateTime.Parse(startingTime));
- Console.WriteLine(t1);
- }
Final output of this small program:-
Note:- Time is depend on your Machine Performance.
Final Word
I hope this small program will help in your programming acknowledge about the IF and Conditional Expression. If have any query free feel drop your comment in comment box.

Join the conversation! Your thoughts help the community grow.