Ternary Operator Error [duplicate] - c#

This question already has answers here:
Method Call using Ternary Operator
(8 answers)
Closed 8 years ago.
I want to convert this if, else-if, can someone help me out please?
if (condition1)
response.Redirect(" some link");
else if (condition2)
response.Redirect("link 2");
I want convert above statement,but showing error at the end, required ":". Any other way i can use this?
LinkPurchase.PostBackUrl =((Condition)?string.Format("some link"):
(condition2)?string.Format("link 2));

You cannot rewrite that to the ?: operator.
You have an if-else if, not just an if-else.
Besides, you do not pick up the return values from the Redirect calls.
The usual case where you do want to rewrite to the ?: operator is:
if (condition)
something = Abc();
else
something = Xyz();
where it is natural to use instead:
something = condition ? Abc() : Xyz();

You cannot do this.
There are other answers here that inform you that the ?: operator needs the "else" part, so yes, the first problem is that you're missing that.
However, Response.Redirect doesn't return anything, so you cannot do this even with the else part.
?: is an expression, you cannot write statements (easily) with this.
Stick with the if-statements.

Related

what is the use of ?? in c# [duplicate]

This question already has answers here:
What do two question marks together mean in C#?
(19 answers)
Closed 5 years ago.
I know ? checks for null when placed before a . member access and ?: for conditional statements. Although, I think ?? checks for null as well but I'm not very sure
I can't find useful information about ?? on https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/
PS. Actually I didn't look well at the MSDN reference very well. I've just seen its definition now.
I though of closing this post before but I won't for the sake of anyone who wouldn't think of referring to ?? as double question marksin their question
that operator is sugarSyntax for operations with nullable operands
The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.
int? counter = null;
int backup = counter ?? 0;
in this case backup will be assigned with counter value IF counter is different to null, for something ELSE then backup will be assigned with 0
note that I bold the keywords IF-ELSE which make us infer that ?? operand can be replaced by simple old-school if else conditionals.

Recursion, and the difference between -- and i-1 [duplicate]

This question already has answers here:
pre Decrement vs. post Decrement
(2 answers)
Closed 6 years ago.
I was doing some C# practice and decided to make a basic function to sum the contents of an integer array.
Originally I wrote my code as follows:
if(index == 0)
return toSum[index];
else
return toSum[index] + sum(toSum, index--);
Now that code resulted in a StackOverFlow exception. This made no sense to me; surely this is how one would do a summation? Turns out the problem was in the index--. When I changed it to index - 1 it worked out fine, thus I was wondering why is that the case? My understanding is that it is simply a shorthand for index = index-1. I was wondering if anyone could explain the reason behind this behavior.
Post-decrement operator returns the value before decrementing, so in your case the index will never be 0 and the function won't stop calling itself and you'll get a stack overflow. You want to write --index instead. It will return the value after decrementing then.

How does the tilde operator work in c# [duplicate]

This question already has answers here:
What does the tilde mean in an expression? [duplicate]
(6 answers)
Closed 7 years ago.
intStyle = intStyle & ~(WS_MINIMIZE);
It is the first time I see this , I am trying to learn how to hook low lvl APIs to C# and make some calls , and I do not understand what this line means. Thank you guys!
It's an operation on a flag. You need to understand bitoperations (AND, OR, NOT, XOR..) for that. This line deletes the flag WS_MINIMIZE from the intStyle flagmask. More reading: Using Bitwise operators on flags , http://www.codeproject.com/Articles/13740/The-Beginner-s-Guide-to-Using-Enum-Flags.
See this for the & operator.
And this for the ~ operator
They are bitwise operators. The first one is a bitwise AND. The second one performs a bitwise complement operation.
This is a bitwise operation.
See for example http://www.codeproject.com/Articles/544990/Understand-how-bitwise-operators-work-Csharp-and-V

.Net Syntax - what does "=>" do when filtering text within a collection? [duplicate]

This question already has answers here:
What does '=>' do in C#? [duplicate]
(5 answers)
Closed 8 years ago.
Say I have a collection that I want to filter on...
var users = groupInner.GetMembers(true).Where(user => user.Name.ToUpper().StartsWith("D0") == false &&
user.Name.ToUpper().StartsWith("D1") == false &&
user.Name.ToUpper().StartsWith("D2") == false &&
user.Name.ToUpper().StartsWith("D3") == false &&
user.Name.ToUpper().StartsWith("D4") == false).ToList();
When filtering with the where clause on text, I can only get the statement to work with =>. == doesn't seem to work. I've tested it, the above code gives me my correct answer, but just for understanding and to find out if I should using something more appropriate than what I already have...what does => mean when comparing text and is there something I should be using instead?
That's the syntax for indicating a lambda expression. It's called the "lambda operator". See:
http://msdn.microsoft.com/en-us/library/bb397687.aspx
To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator =>, and you put the expression or statement block on the other side.

What is the VB.NET equivalent of the C# ? operator? [duplicate]

This question already has answers here:
Is there a conditional ternary operator in VB.NET?
(5 answers)
Closed 9 years ago.
What is the VB.NET equivalent of the C# ? operator?
For example, how would the following code be written in VB.NET?
hp.pt = iniFile.GetValue("System", "PT").ToUpper().Equals("H") ? PT.PA : PT.SP
Historically, IIf was commonly used for that - but that does not use short-circuiting so is not quite the same. However, there is now a 3-part If:
hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
that does use short-circuiting, and thus is identical to the conditional operator in C#.
You can use the If operator
hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
Try using the If function like so:
x = If(condition, trueValue, falseValue)
This question is a duplicate of a question that has already been asked and answered:
Is there a conditional ternary operator in VB.NET?
here:
Dim foo as String = If(bar = buz, cat, dog)

Categories

Resources