3

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"))
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
POSH Guy
  • 1,798
  • 2
  • 11
  • 15
  • 2
    Possible duplicate of [How does the ternary operator work?](https://stackoverflow.com/questions/463155/how-does-the-ternary-operator-work) – ProgrammingLlama Apr 02 '19 at 04:17
  • 2
    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. – madreflection Apr 02 '19 at 04:18

3 Answers3

5

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

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

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");
}
Stefan Becker
  • 5,695
  • 9
  • 20
  • 30
Aleonna
  • 1
  • 1
0

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

leox
  • 1,315
  • 17
  • 26