Conditional operator (ternary operator) - CSharp Language Basics

CSharp examples for Language Basics:Operator

Introduction

The conditional operator has the form q ? a : b, where if condition q is true, a is evaluated, else b is evaluated. For example:

Demo Code

using System;/*from   ww w  .  j a  v a2s  .c o  m*/
class Test
{
   static int Max (int a, int b)
   {
      return (a > b) ? a : b;
   }
   static void Main(){
      Console.WriteLine(Max(2,1));
   }
}

Result


Related Tutorials