0

I am working on a project where I found a syntax heavy line of code in it

logonuser = logonuser.IndexOf(domain) >= 0 ? logonuser : domain + "\\" + logonuser;

Both logonuser and domain are Strings. I need an explanation of what it is and how does it work?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Matty
  • 77
  • 1
  • 9
  • It's assigning a value of `logonuser` to itself if the domain is found in the string, otherwise it is prepending the domain and assigning it to `logonuser`. – Jane S Oct 02 '14 at 03:23
  • possible duplicate of [Benefits of using the conditional ?: (ternary) operator](http://stackoverflow.com/questions/3312786/benefits-of-using-the-conditional-ternary-operator) – John Kugelman Oct 02 '14 at 03:23

2 Answers2

3

this is the ternary operator

it's like saying:

if(logonuser.IndexOf(domain) >= 0)
{
     logonuser = logonuser;
}
else
{
     logonuser = domain + "\\" + logonuser;
}
RadioSpace
  • 953
  • 7
  • 15
0

As others already stated, that's the ternary operator. The explanation of the code besides the fact that is using this operator is:

  • If longuser is a "absolute"(*) url containing the domain, then use it without changing it.

  • else (longuser is a relative url) build a absolute url using the domain

(*) Not really absolute without the protocol

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155