Here is the syntax of using C# if else statement in a single line.
- (Condition) ? "Value For true":" Value For False "
- if(x==1)
- {
- x=10;
- }
- else
- {
- x=15;
- }
- x=(x==1)?10:15;
Here is a detailed tutorial, How To Use C# If Else Statement
You can use C# if else statement in a single line. Here is the code sample.
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegisterMembers onlyeBook · C#Modern CSharp Mastery - Advanced Guide for Experienced DevelopersDownload nowWrite a blog post and earn +250 SHARP when it is approved.
Write a blog
Join the conversation! Your thoughts help the community grow.