POSH Guy
POSH Guy

Reputation: 2048

Explanation of if statement

I am new in C#. looks like below code is in C. Could some please explain below if statement

if ((!csentry.get_Item("UR.Action").get_IsPresent() ? false : csentry.get_Item("UR.Action").get_Value() == "Disable"))

Upvotes: 3

Views: 168

Answers (3)

Aleonna
Aleonna

Reputation: 1

An if statement basically says if a condition is met, do this. In this instance, it is saying:

IF !csentry.get_Item("UR.Action").get_IsPresent() ? false : csentry.get_Item("UR.Action").get_Value() is equal to "Disable", do a result that isn't included.

Really more info is needed to give you the exact answer. But once you realize how they work, if statements are simple. The basic format is:

if (condition) {
   result
}

Say I had to do a simple response program for a someone saying "hi"

if (input == 'hi') {
   printf("hello");
}

Upvotes: 0

TheGeneral
TheGeneral

Reputation: 81493

It breaks down to the following. Which is 1000 times more readable than what you had

var urlAction = csentry["UR.Action"];

if (urlAction.IsPresent && urlAction.Value == "Disable")
{
     // then do stuff
}

Note : see @madreflection's comments on the post for an understanding of why this maybe mangled so badly

it's code that's been compiled and then decompiled back to C# without access to referenced assemblies. This often happens in ILSpy; when you add the referenced assemblies and the types become available, the property metadata allows it to translate to property accesses


Additional Resources

?: Operator (C# Reference)

The conditional operator ?:, commonly known as the ternary conditional operator, evaluates a Boolean expression, and returns the result of evaluating one of two expressions, depending on whether the Boolean expression evaluates to true or false

Upvotes: 5

leox
leox

Reputation: 1345

Here as for If it's evaluating a ternary operation.

From a very high level, it looks like !csentry.get_Item("UR.Action").get_IsPresent() if the UR.Action is present then the if condition will not executed. And if it not present then this condition get evaluated csentry.get_Item("UR.Action").get_Value() == "Disable" if that true then the if block get executed else it'll not get executed.

When you get time take a look at ternary_operator for more details

Upvotes: 0

Related Questions