Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  Asp.net  >  正文 asp.net运算符之逻辑运算符以及其他运算符

asp.net运算符之逻辑运算符以及其他运算符

发布时间:2017-12-12   编辑:www.jquerycn.cn
jquery中文网为您提供asp.net运算符之逻辑运算符以及其他运算符 等资源,欢迎您收藏本站,我们将为您提供最新的asp.net运算符之逻辑运算符以及其他运算符 资源
在.net中运算符分类很多种类型,包括有我们常用的boolean型运算符,通用的运行符有 ==、!=、<、>、<=、>=、binary 、binary -、^、&、|、~、 、-- 和 sizeof()。

逻辑(布尔型)运算符用于对boolean型的结果的表达式进行运算,运算的结果都是boolean型。其运算结果如下所示:

运算符 运算 例子 结果
& AND(与) false&true false
| OR(或) false|true true
^ XOR(异或) false^true true
! NOT(非) !false true
&& AND(短路) false&&true false
|| OR(短路) false||true true


下面对一些比较容易出现问题进行简单解释:

1、“^“ 将计算操作数的逻辑“异或”;也就是说,当且仅当只有一个操作数为 true时,结果才为true。

2、“&”与”&&“的区别在于,如果使用前者连接,那么无论任何情况下,“&”两边的表达式都会参与计算。如果使用后者连接,当“&&”的左边为false,则将不会计算其右边的表达式。

3、“|”与“||”的区别在于,“|”表示两边任何一个布尔表达式为真,该组合就会返回true值;而对于“||”,跟第二差不多,若左边是true则返回true,若左边是falsh,则看右边,若右边为true则为true否则为falsh。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4707')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4707>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        int x = 0;
        string name = "搜索吧";

        //&运算,两个中任何一个为假时则为假
        Response.Write("x != 0 & name = /"搜索吧/"的运算结果是:" (x != 0 & name == "搜索吧"));

        Response.Write("<br>");

        //|运算,当两个中任何一个为真时,运算结果为真,否则为加
        Response.Write("x != 0 | name = /"搜索吧/"的运算结果是:" (x != 0 | name == "搜索吧"));

        Response.Write("<br>");

        //^运算,当且仅当有一个是True时,为真,否则为假
        Response.Write("x != 0 ^ name = /"搜索吧/"的运算结果是:" (x != 0 ^ name == "搜索吧"));
        Response.Write("<br>");
        Response.Write("x == 0 ^ name = /"搜索吧/"的运算结果是:" (x == 0 ^ name == "搜索吧"));
        Response.Write("<br>");

        //!运算,若真则假,若假则真
        Response.Write("x != 0 的运算结果是:" !(x != 0));

        Response.Write("<br>");

        //&&短路运算,若左边为假则退出,若左边为真,则看右边
        Response.Write("x != 0 && name = /"搜索吧/"的运算结果是:" (x != 0 && name == "搜索吧"));

        Response.Write("<br>");
        //||短路运算,若左边为真,则为真退出;若左边为假,则看右边,右边为真,则为真,否则为假
        Response.Write("x != 0 || name = /"搜索吧/"的运算结果是:" (x != 0 || name == "搜索吧"));
    }
}

 

运算符

     C# 提供大量运算符,这些运算符是指定在表达式中执行哪些操作的符号。通常允许对枚举进行整型运算,例如 ==、!=、<、>、<=、>=、binary 、binary -、^、&、|、~、 、-- 和 sizeof()。此外,很多运算符可被用户重载,由此在应用到用户定义的类型时更改这些运算符的含义。
 
<table style="width: 580px; height: 925px"> <tbody> <tr> <th>运算符类别</th> <th>运算符</th> </tr> <tr> <td>
基本
</td> <td>
x.y
f(x)
a[x]
x
x--
new
typeof
checked
unchecked
->
</td> </tr> <tr> <td>
一元
</td> <td>
-
!
~
x
--x
(T)x
True
False
&
sizeof
</td> </tr> <tr> <td>
乘法
</td> <td>
*
/
%
</td> </tr> <tr> <td>
加法
</td> <td>
-
</td> </tr> <tr> <td>
变换
</td> <td>
<<
>>
</td> </tr> <tr> <td>
关系和类型检测
</td> <td>
<
>
<=
>=
is
as
</td> </tr> <tr> <td>
相等
</td> <td>
==
!=
</td> </tr> <tr> <td>
逻辑“与”
</td> <td>
&
</td> </tr> <tr> <td>
逻辑 XOR
</td> <td>
^
</td> </tr> <tr> <td>
逻辑“或”
</td> <td>
|
</td> </tr> <tr> <td>
条件 AND
</td> <td>
&&
</td> </tr> <tr> <td>
条件 OR
</td> <td>
||
</td> </tr> <tr> <td>
条件运算
</td> <td>
?:
</td> </tr> <tr> <td>
赋值
</td> <td>
=
=
-=
*=
/=
%=
&=
|=
^=
<<=
>>=
??
</td> </tr> </tbody> </table> <ul> <li>运算符优先级 <ol> <li>前 前-- (正号) -(负号)! ~</li> <li>* / %</li> <li> -</li> <li><< >></li> <li>< > <= >=</li> <li>== !=</li> <li>&</li> <li>^</li> <li>|</li> <li>&&</li> <li>||</li> <li>赋值运算</li> <li>后 后--</li> </ol> </li> </ul>
<ul> <li>可重载运算符</li> </ul>
     C# 允许用户定义的类型通过使用 operator 关键字定义静态成员函数来重载运算符。但不是所有的运算符都可被重载,下表列出了不能被重载的运算符:
<table> <tbody> <tr> <th>运算符</th> <th>可重载性</th> </tr> <tr> <td>
、-、!、~、 、--、true 和 false
</td> <td>
可以重载这些一元运算符。
</td> </tr> <tr> <td>
, -, *, /, %, &, |, ^, <<, >>
</td> <td>
可以重载这些二进制运算符。
</td> </tr> <tr> <td>
==, !=, <, >, <=, >=
</td> <td>
比较运算符可以重载(但请参见本表后面的说明)。
</td> </tr> <tr> <td>
&&, ||
</td> <td>
条件逻辑运算符不能重载,但可使用能够重载的 & 和 | 进行计算。
</td> </tr> <tr> <td>
[]
</td> <td>
不能重载数组索引运算符,但可定义索引器。
</td> </tr> <tr> <td>
()
</td> <td>
不能重载转换运算符,但可定义新的转换运算符(请参见 explicit 和 implicit)。
</td> </tr> <tr> <td>
=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
</td> <td>
赋值运算符不能重载,但  = 可使用   计算,等等。
</td> </tr> <tr> <td>
=、.、?:、->、new、is、sizeof 和 typeof
</td> <td>
不能重载这些运算符。
</td> </tr> </tbody> </table> <ul> <li>语法</li> </ul>
<table border="1" cellspacing="0" cellpadding="2"> <tbody> <tr> <td valign="top"> <pre><span>public static Complex operator (Complex c1, Complex c2) </span></pre> </td> </tr> </tbody> </table>

您可能感兴趣的文章:
VBScript 运算符
javascript的常用运算符
VBScript的运算符
php运算符及运算符优先级-php入门教程(4)
javascript中的逻辑运算符
php中比较常用的特殊运算符号和函数
php中运算符的优先级是什么?
《Go语言四十二章经》第九章 运算符
asp.net运算符之逻辑运算符以及其他运算符
Go 语言运算符

[关闭]