1

Hi need a little help understanding the following statement and the logic of what its doing.

AuthenticationProvider auth = 
    (level & Levels.Authentication) == Levels.Authentication
        ? GetAuthenticationProviderByName(authentication, authPhrase)
        : DefaultAuthenticationProvider.Instance;

I see im making an AuthentcationProvider, but then I dont understand whats happening why is there a comparison there and whats is the question mark doing?

Thanks.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
squareborg
  • 1,562
  • 14
  • 18
  • This is what the question mark is doing: http://stackoverflow.com/questions/3312786/benefits-of-using-the-conditional-ternary-operator – Greg Hewgill Oct 16 '11 at 18:25
  • 1
    Google is your friend, you know. – Louis Kottmann Oct 16 '11 at 18:30
  • 2
    @Baboon did you ever try googling for `?` and `:`? – Uwe Keim Oct 16 '11 at 18:36
  • @Baboon google is alright if you know what your looking for, I couldn't tell what it was logically doing and mark byers answer puts it in a logically clear manor. Thanks for your two cents – squareborg Oct 16 '11 at 18:40
  • 1
    It's called a question mark, third result on google when you type "C# question mark". – Louis Kottmann Oct 16 '11 at 19:17
  • @Baboon how would you use your excellent google craft to extract the logical path of the statement. – squareborg Oct 16 '11 at 19:27
  • I wouldn't qualify 3 words in google and a button pressed as "excellent". Which is why i downvoted. Now the matter is closed, you got your answer, next time give it an honest try before asking a question. Happy coding. – Louis Kottmann Oct 16 '11 at 20:57
  • @Baboon yet again you decided not to answer the question but make a smart ass remark – squareborg Oct 16 '11 at 21:11
  • When you downvote, SO recommands to comment to explain why. I'm explaining. What's the problem now? You're a ball of anger man... – Louis Kottmann Oct 16 '11 at 21:58
  • @Baboon I'm not angry, You down voted because you though that I could google the answer to this, and I'm challenging that. If you can show me how to google for this you win. – squareborg Oct 17 '11 at 08:17
  • Like i said, the third result on google when you type "C# question mark" answers your question. (google adapts the result for the user, so you will probably find another site/blog than me, but you will find a result). – Louis Kottmann Oct 17 '11 at 09:02

5 Answers5

2

The ? and : here is called the conditional operator (it is also sometimes known as the ternary operator).

The code you posted is equivalent to this:

AuthenticationProvider auth;
if ((level & Levels.Authentication) == Levels.Authentication) { 
    auth = GetAuthenticationProviderByName(authentication, authPhrase);
} else {
    auth = DefaultAuthenticationProvider.Instance;
}

The & is a "bitwise and". Here it used here to test to see if the Levels.Authentication bit is set.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

It's the Ternary operator:

condition ? first_expression : second_expression;

So like a short version of if...else.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
1

Looks like it's doing a bit-wise compare between level and Levels.Authentication. I'm assuming level is a "flags" variable, and it's checking if the Levels.Authentication flag is on.

If so it does the GetAuthenticationByName, if not it does the DefaultAuthenticationProvider.Instance.

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
1

level is a variable of type Levels, which is an enum decorated with the FlagsAttribute.

This means that you can combine multiple values of that enum in one variable.

The line level & LevelsAuthentication == Levels.Authentication, checks if the level variable has the value Levels.Authentication set.

If this is true, the GetAuthenticationProviderByName(authentication, authPrase) statement is executed. if it is false, the DefaultAuthenticationProvider.Instance is returned.

For more information check out:

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
0

?:

is the conditional operator, see here: ?: Operator (C# Reference)

it works like this:

s = x != 0.0 ? Math.Sin(x)/x : 1.0;

assigning to s Math.Sin(x)/x only if x != 0

Davide Piras
  • 43,984
  • 10
  • 98
  • 147