Pavle Davitković’s Post

View profile for Pavle Davitković, graphic

Software Developer @Ingsoftware

If you think if/else is the only solution for true/false results… …stop scrolling to learn about alternative. The ternary operator is a conditional operator in C# that works the same as the if/else statement. To work properly, it requires three operands: - Condition - Consequent - Alternative You won’t be able to evaluate with only possibility. If the condition is true, the consequent will be returned, otherwise, it will return an alternative. They are target-typed and they can also work with var. But in both cases, results should be that can be an implicit converted from one type to the other. They remove multiple lines of code. But in the case of a large number of conditions, they can make code messy and unreadable. So keep in mind… They are good alternative. But for simpler if/else statements... For more complex statements, go with the switch expression.

  • No alternative text description for this image
Pavle Davitković

Software Developer @Ingsoftware

6mo

Repost and spread the word with your network ♻️

Eric Jonathan

Engineering Manager @ RUPIAH CEPAT | Full-stack Developer, Hackathon Mentor | A BJJ White Belt

6mo

> 5 == "Express"? 🤔 . Syntax is ok though. 👍

Cosmin Vladutu

Senior .NET Fullstack Developer • Leader • Contractor • Freelancer | I help organizations in crafting software products while also strengthening their teams.

6mo

Ternary operators are nice, but they are just syntactic sugar. I wouldn't "block" a PR only for this. Some might say it's more readable, but to be honest, since we can't put a number on both options, I don't see any difference.

Milan Jovanović

Practical .NET and Software Architecture Tips | Microsoft MVP

6mo

return order.DeliverMethod()

Kristijan Kralj

I help .NET developers advance their careers by mastering the latest tech and industry best practices.

6mo

Nah, let's use a Strategy pattern 😅

Stefan Đokić

➡️ I help you to improve your .NET knowledge! | Microsoft MVP‎

6mo

Okay, I stopped to scroll. 😁 Old but gold

Evan Howlett

Software Engineer | SQL, F#, C#

6mo

Ternaries are great for conditionally setting values, but if you need conditional execution of behavior, they are not for you. The great advantage of ternaries is that they prevent the jumping instructions introduced with if/else statements so they're awesome in hot paths!

Rodrigo H. Cordeiro

Desenvolvedor Back-End C# | .NET | SQL - Cursando Análise e Desenvolvimento de Sistemas no Senac

6mo

I really love use ternary operator. It's elegant

Mario MARTIN

Everything necessary but mostly .Net

6mo

Switch it is 😃

Md. Saddam Hossain

Senior Software Engineer | C# | ASP.NET Core | Web API | Blazor | SQL Server | Azure | DevOps | Passionate .NET Developer 💙

6mo

Nice things! "Old is Gold" my friend, Pavle Davitković. 😊

See more comments

To view or add a comment, sign in

Explore topics