Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ?: Expression

I have a function with multiple if's (THIS IS NOT THE ACTUAL CODE)

if(n == 1)
   m = 1;
if(n == 2)
   m = 2;
if(n == 3)
   m = 3;

Instead of that I wanted to do make them all into ?: expression :

(n == 1) ? m = 1;

But it says that its expecting a ':'

I am familiar with the ?: expression from C++ where you can simply write:

(n == 1) ? m = 1 : 0;

But 0 doesn't take here. This is a ridiculous question and I couldn't even find an answer in google since it ignores '?:' as a word.

ANSWER : too bad the answer was in the comments. There is no way to "do nothing" in this expression and I should use if-else or switch. thanks.

like image 204
user779444 Avatar asked Jan 21 '12 18:01

user779444


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

It looks like you're looking for:

m = (n == 1) ? 1 : 0;

Which you could then cascade to:

m = (n == 1) ? 1 : (n == 2) ? 2 : (n == 3) ? 3 : 0;

An important (to me, anyway), aside:

Why are you asking this? If it's because you think that this form will be more efficient than a series of if statements, or a switch, don't. The C# compiler and the .net JIT compiler are really quite clever and they'll transform your code (hopefully!) into its most optimal form. Write your code so its as understandable by yourself, or the developer who has to maintain it after you as it can be. If the performance you get isn't acceptable, then try changing it around but measure to determine what works best (bearing in mind that newer compilers/.net frameworks could well change what happens).

like image 190
Rob Avatar answered Sep 28 '22 08:09

Rob


looking for ternary operator in c# will give you relevant results.

an example usage would be

var m = n == 1 ? 1 : 0
like image 42
flq Avatar answered Sep 28 '22 07:09

flq