+ 1

whan can do write a operator same "or", but just any of two operant must be true ?

4th Nov 2016, 11:13 PM
KiynL
KiynL - avatar
5 Respuestas
+ 6
Like what Harry said, in C# operator, or is represented by || When to use 'or'? In a real world context, for example you only want the admin/moderators to execute the command/system/button, you can use the 'or' symbol instead of rewriting the same code twice for admin and moderator seperately. For example : private void //Command or button { .addcheck(User.Admin || User.Moderator) //^^^ of course it wont work, its and example //That checks the user role, if the user role does not meet any of the requirement, it will stop process, etc .Do(async...{}) // or whatever that executes some command } And yes, only 1 of the requirements need to be met in order for the whole thing to be true.
5th Nov 2016, 4:34 AM
Wen Qin
Wen Qin - avatar
+ 6
No, in C# there is no function to provide a 'return false if both operators are true in 'or' ' But there is a way to code it in a more complex manner. int x = 1; int y = 2; if ( !(x == 1) || !(y == 2) ) { Console.WriteLine("1"); } else { Console.WriteLine("2"); } But this one I made has its flaws, if x is not 1 and y is not 2 , it will still return true. But I hope that someone will post a better verison of it or you can find the solution for it too.
5th Nov 2016, 5:10 AM
Wen Qin
Wen Qin - avatar
+ 2
In c# the or operator is given by ||. For example: int x = 4; int y = 10; if(x == 4 || y == 5) { //some code } The code will execute since one condition is true.
4th Nov 2016, 11:19 PM
Harry Pearson
Harry Pearson - avatar
+ 1
thanks friend but i think i dont recieve my mean. in example: if (a == true || b == false ) \\return true if (a == false || b == true ) \\return true but .. if both operant are true (a == true || b == true) \\return false i seen this operator in some other language that its name was "xor" . but i don't know c# have this shortcut operator or no ?
5th Nov 2016, 4:53 AM
KiynL
KiynL - avatar
0
a!=b
9th Nov 2016, 3:02 PM
Cheng Xiongfeng
Cheng Xiongfeng - avatar