c#中“||”的用法是什么?
展开全部
条件“或”运算符 (||) 执行 bool 操作数的逻辑“或”运算,但仅在必要时才计算第二个操作数。
x || y
当x、y中的任意一个满足时返回true。如果x满足,则不执行y判断,直接返回true
比如:
MSDN例子:
class ConditionalOr
{
static bool Method1()
{
Console.WriteLine("Method1 called");
return true;
}
static bool Method2()
{
Console.WriteLine("Method2 called");
return false;
}
static void Main()
{
Console.WriteLine("regular OR:");
Console.WriteLine("result is {0}", Method1() | Method2());
Console.WriteLine("short-circuit OR:");
Console.WriteLine("result is {0}", Method1() || Method2());
}
}
/*
Output:
regular OR:
Method1 called
Method2 called
result is True
short-circuit OR:
Method1 called
result is True
*/
x || y
当x、y中的任意一个满足时返回true。如果x满足,则不执行y判断,直接返回true
比如:
MSDN例子:
class ConditionalOr
{
static bool Method1()
{
Console.WriteLine("Method1 called");
return true;
}
static bool Method2()
{
Console.WriteLine("Method2 called");
return false;
}
static void Main()
{
Console.WriteLine("regular OR:");
Console.WriteLine("result is {0}", Method1() | Method2());
Console.WriteLine("short-circuit OR:");
Console.WriteLine("result is {0}", Method1() || Method2());
}
}
/*
Output:
regular OR:
Method1 called
Method2 called
result is True
short-circuit OR:
Method1 called
result is True
*/
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询