0

This is an odd question but in someones else's answer in a different question they posted the following

try
{
   PingReply reply = pinger.Send(nameOrAddress);
   pingable = reply.Status == IPStatus.Success;
}

How does pingable = reply.Status == IPStatus.Success; work? To me that looks like an if statement without the if.

M4N
  • 94,805
  • 45
  • 217
  • 260
Jeebwise
  • 177
  • 2
  • 2
  • 12
  • 3
    `pingable` is a boolean variable. See [this question](http://stackoverflow.com/q/18183591) from earlier today. – H H Aug 12 '13 at 18:58
  • You should really include a link to the answer in question. – Servy Aug 12 '13 at 19:05

6 Answers6

10
reply.Status == IPStatus.Success 

will return a boolean which will be assigned to pingable variable.

Same thing will happen inside if statement: First the expression will be calculated, with true or false as a result, and only the result will be checked as branch condition.

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
  • Thank you. I did not know that you could do stuff like this without actually writing out an if statement. I am always looking for ways to make code look cleaner. – Jeebwise Aug 13 '13 at 00:40
3

Same as

if(reply.Status == IPStatus.Success)
    pingable = true;
else
    pingable = false;

The code

reply.Status == IPStatus.Success

returns a boolean that is inserted in pingable.

the_lotus
  • 12,668
  • 3
  • 36
  • 53
2

(reply.Status == IPStatus.Success) evaluates as a boolean true or false

randcd
  • 2,263
  • 1
  • 19
  • 21
1

the operator == always resolves to a boolean

so

pingable = reply.Status == IPStatus.Success;

puts true in pingable if reply.Status and IPStatus.Success are equal, and it puts false in pingable if they are not.

1

It is a condition. pingable is boolean.

FelProNet
  • 689
  • 2
  • 10
  • 25
0

pingable (as boolean) is set to if reply.Status is equal to IPStatus.Sucess

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284