C#每日一课(七)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/advent86/article/details/81713859

C#运算符

运算符是一种告诉编译器执行特定数字或逻辑的操作符,C#中内置的运算符如下:

  1. 算术运算符
  2. 关系运算符
  3. 逻辑运算符
  4. 位运算符
  5. 赋值运算符
  6. 其它运算符

 

算术运算符

假定有变量a和b分别值是10和20

运算符

描述

实例结果

+

把两个操作数相加

a+b,得到的值是30

-

从第一个操作数中减去第二个操作数

a-b,得到的值是-10

*

把两个操作数相乘

a*b,得到的值是200

/

把第一个操作数除以第二个操作数

b/a,得到的值是2

%

取模运算符,整除后的余数

b%a,得到的值是0

++

自增运算符,整数值增加1

a++,得到的值是11

--

自减运算符,整数值减少1

a--,得到的值是9

新建项目,选择Visual C#---->Windows桌面---->控制台应用(.Net Framework)

工程名称:Chapter02_001

在Main方法中添加代码后,整体代码如下

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace Chapter02_001

{

    class Program

    {

        static void Main(string[] args)

        {

            int a = 10, b = 20;

            int result;

            Console.WriteLine("-------------------运算符测试---------------------");

            //+运算符

            result = a + b;

            Console.WriteLine("+运算符:{0}+{1}={2}", a, b, result);

            result = b - a;

            Console.WriteLine("-运算符:{0}-{1}={2}", b, a, result);

            result = a * b;

            Console.WriteLine("*运算符:{0}*{1}={2}", a, b, result);

            result = b / a;

            Console.WriteLine("/运算符:{0}/{1}={2}", b, a, result);

            result = b % a;

            Console.WriteLine("%运算符:{0}%{1}={2}", b, a, result);

            result = a++;

            Console.WriteLine("++运算符:{0}++={1}", a, result);

            result = a--;

            Console.WriteLine("--运算符:{0}--={1}", a, result);



            Console.ReadLine();

        }

    }

}

编译运行后结果如下:

注意一下最后的两个结果:

a++,为什么变成了这个时候变成了11,而结果是10?因为a++后a的值已经变成了原来的值10+1等于11了,但是这里是后加加,表达式的结果是先取了a原来的值作为一表达式的结果,再对a进行运算,所以结果是10。

  1. -与上面同理

如果是++、--有前表示前缀+、--这个时候表达式的结果就是对a运算完的结果作为表达式的结果就会与上面不一样。

 

关系运算符

下面是C#中的关系运算符,假定有a、b两个变量,它们的值分别为10、20

运算符

描述

实列结果

==

检查两个操作数是否相等,如果相等则表达式为真

(a==b),得到的结果为假

!=

检查两个操作数的值是否相等,如果不相等时表达式为真

(a!=b),得到的结果为真

>

检查左边的操作数是否大于右边操作数的值,如果是则表达式为真

(a>b),得到的结果为假

<

检查左边的操作数是否小于右边操作数的值,如果是则表达式为真

(a<b),得到的结果为真

>=

检查左边的操作数是否大于等于右边操作数的值,如果是则表达式为真

(a>=b),得到的结果为假

<=

检查左边的操作数是否小于等于右边操作数的值,如果是则表达式为真

(a<=b),得到的结果为真

在Main方法中添加如下代码:

Console.WriteLine("-------------------关系运算符测试---------------------");

    Console.WriteLine("a==b? {0}", (a==b));

    Console.WriteLine("a!=b? {0}", (a != b));

    Console.WriteLine("a>b? {0}", (a > b));

    Console.WriteLine("a<b? {0}", (a < b));

    Console.WriteLine("a>=b? {0}", (a >= b));

Console.WriteLine("a<=b? {0}", (a <= b));

 

Main方法的整体代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace Chapter02_001

{

    class Program

    {

        static void Main(string[] args)

        {

            int a = 10, b = 20;

            int result;

            Console.WriteLine("-------------------算述运算符测试---------------------");

            //+运算符

            result = a + b;

            Console.WriteLine("+运算符:{0}+{1}={2}", a, b, result);

            result = b - a;

            Console.WriteLine("-运算符:{0}-{1}={2}", b, a, result);

            result = a * b;

            Console.WriteLine("*运算符:{0}*{1}={2}", a, b, result);

            result = b / a;

            Console.WriteLine("/运算符:{0}/{1}={2}", b, a, result);

            result = b % a;

            Console.WriteLine("%运算符:{0}%{1}={2}", b, a, result);

            result = a++;

            Console.WriteLine("++运算符:{0}++={1}", a, result);

            result = a--;

            Console.WriteLine("--运算符:{0}--={1}", a, result);



            Console.WriteLine("-------------------关系运算符测试---------------------");

            Console.WriteLine("a==b? {0}", (a==b));

            Console.WriteLine("a!=b? {0}", (a != b));

            Console.WriteLine("a>b? {0}", (a > b));

            Console.WriteLine("a<b? {0}", (a < b));

            Console.WriteLine("a>=b? {0}", (a >= b));

            Console.WriteLine("a<=b? {0}", (a <= b));



            Console.ReadLine();

        }

    }

}

代码编译运行后结果如下:(False:假;True:真)

 

逻辑运算符

下面为C#中支持的逻辑运算符,假定有变量a、b分别为True 和 False

&&

逻辑与运算符两个操作数都是真是为真,否则为假

(a&&b),表达式的结果为False

||

称为逻辑或运算符,如果两个操作数中有一个为真时,则为真

(a||b),表达式的结果为True

!

称为逻辑非运算符,如果操作数为真则结果为假,如果操作数为假则结果为真

(!a),表达式的结果为True

在Main方法中加入如下代码进行测试

bool x = true, y = false;

     Console.WriteLine("-------------------逻辑运算符测试---------------------");

     Console.WriteLine("{0}&&{1}:{2}", x, y, (x && y));

     Console.WriteLine("{0}||{1}:{2}", x, y, (x || y));

Console.WriteLine("!{0}:{1}", x, (!y));

 

Main中整体代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace Chapter02_001

{

    class Program

    {

        static void Main(string[] args)

        {

            int a = 10, b = 20;

            int result;

            Console.WriteLine("-------------------算述运算符测试---------------------");

            //+运算符

            result = a + b;

            Console.WriteLine("+运算符:{0}+{1}={2}", a, b, result);

            result = b - a;

            Console.WriteLine("-运算符:{0}-{1}={2}", b, a, result);

            result = a * b;

            Console.WriteLine("*运算符:{0}*{1}={2}", a, b, result);

            result = b / a;

            Console.WriteLine("/运算符:{0}/{1}={2}", b, a, result);

            result = b % a;

            Console.WriteLine("%运算符:{0}%{1}={2}", b, a, result);

            result = a++;

            Console.WriteLine("++运算符:{0}++={1}", a, result);

            result = a--;

            Console.WriteLine("--运算符:{0}--={1}", a, result);



            Console.WriteLine("-------------------关系运算符测试---------------------");

            Console.WriteLine("a==b? {0}", (a==b));

            Console.WriteLine("a!=b? {0}", (a != b));

            Console.WriteLine("a>b? {0}", (a > b));

            Console.WriteLine("a<b? {0}", (a < b));

            Console.WriteLine("a>=b? {0}", (a >= b));

            Console.WriteLine("a<=b? {0}", (a <= b));



            bool x = true, y = false;

            Console.WriteLine("-------------------逻辑运算符测试---------------------");

            Console.WriteLine("{0}&&{1}:{2}", x, y, (x && y));

            Console.WriteLine("{0}||{1}:{2}", x, y, (x || y));

            Console.WriteLine("!{0}:{1}", x, (!y));



            Console.ReadLine();

        }

    }

}

编译运行后的结果如下:

 

位运算符

位运算符是作用于位,并且逐位执行操作的

p

q

p&q

p|q

p^q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

注意^这个操作符表示按位异或,也就是两个位不一样时值为1,相同时则值为0

位运算符,假定变量a的值是60,变量b的值是13

60和13的二进制分别是

60 = 00111100

13 = 00001101

运算符

描述

实列结果

&

二进制位的与运算

(a&b),它的结果是12 = 00001100

二进制位的按位或

(a|b),它的结果是61 = 00111101

^

二进制位的异或

(a^b),它的结果是49 = 00110001

~

二进制补码运算符,具有位翻转的效果

(~a),它的结果是-61 = 11000011

<<

二进制的左移位

(a<<2),它的结果是240 = 11110000

>>

二进制的右移位

(a>>2),它的结果是15 = 00001111

在Main方法中添加如下代码:

Console.WriteLine("-------------------位运算符测试---------------------");

    a = 60; // 0011 1100

    b = 13; // 0000 1101

    result = a & b;

    Console.WriteLine("{0}&{1} = {2}", a, b, result);

    result = a | b;

    Console.WriteLine("{0}|{1} = {2}", a, b, result);

    result = a ^ b;

    Console.WriteLine("{0}^{1} = {2}", a, b, result);

    result = ~a;

    Console.WriteLine("~{0} = {1}", a, result);

    result = a << 2;

    Console.WriteLine("{0}<<2 = {1}", a, result);

    result = a >> 2;

Console.WriteLine("{0}>>2 = {1}", a, result);

 

整体代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace Chapter02_001

{

    class Program

    {

        static void Main(string[] args)

        {

            int a = 10, b = 20;

            int result;

            Console.WriteLine("-------------------算述运算符测试---------------------");

            //+运算符

            result = a + b;

            Console.WriteLine("+运算符:{0}+{1}={2}", a, b, result);

            result = b - a;

            Console.WriteLine("-运算符:{0}-{1}={2}", b, a, result);

            result = a * b;

            Console.WriteLine("*运算符:{0}*{1}={2}", a, b, result);

            result = b / a;

            Console.WriteLine("/运算符:{0}/{1}={2}", b, a, result);

            result = b % a;

            Console.WriteLine("%运算符:{0}%{1}={2}", b, a, result);

            result = a++;

            Console.WriteLine("++运算符:{0}++={1}", a, result);

            result = a--;

            Console.WriteLine("--运算符:{0}--={1}", a, result);



            Console.WriteLine("-------------------关系运算符测试---------------------");

            Console.WriteLine("a==b? {0}", (a==b));

            Console.WriteLine("a!=b? {0}", (a != b));

            Console.WriteLine("a>b? {0}", (a > b));

            Console.WriteLine("a<b? {0}", (a < b));

            Console.WriteLine("a>=b? {0}", (a >= b));

            Console.WriteLine("a<=b? {0}", (a <= b));



            bool x = true, y = false;

            Console.WriteLine("-------------------逻辑运算符测试---------------------");

            Console.WriteLine("{0}&&{1}:{2}", x, y, (x && y));

            Console.WriteLine("{0}||{1}:{2}", x, y, (x || y));

            Console.WriteLine("!{0}:{1}", x, (!y));



            Console.WriteLine("-------------------位运算符测试---------------------");

            a = 60; // 0011 1100

            b = 13; // 0000 1101

            result = a & b;

            Console.WriteLine("{0}&{1} = {2}", a, b, result);

            result = a | b;

            Console.WriteLine("{0}|{1} = {2}", a, b, result);

            result = a ^ b;

            Console.WriteLine("{0}^{1} = {2}", a, b, result);

            result = ~a;

            Console.WriteLine("~{0} = {1}", a, result);

            result = a << 2;

            Console.WriteLine("{0}<<2 = {1}", a, result);

            result = a >> 2;

            Console.WriteLine("{0}>>2 = {1}", a, result);



            Console.ReadLine();

        }

    }

}

编译运行后的结果如下:

 

C#中的赋值运算符

运算符

描述

实例

=

简单的赋值运算符,把右边操作数的值赋给左边操作数

C = A + B 将把 A + B 的值赋给 C

+=

加且赋值运算符,把右边操作数加上左边操作数的结果赋值给左边操作数

C += A 相当于 C = C + A

-=

减且赋值运算符,把左边操作数减去右边操作数的结果赋值给左边操作数

C -= A 相当于 C = C - A

*=

乘且赋值运算符,把右边操作数乘以左边操作数的结果赋值给左边操作数

C *= A 相当于 C = C * A

/=

除且赋值运算符,把左边操作数除以右边操作数的结果赋值给左边操作数

C /= A 相当于 C = C / A

%=

求模且赋值运算符,求两个操作数的模赋值给左边操作数

C %= A 相当于 C = C % A

<<=

左移且赋值运算符

C <<= 2 等同于 C = C << 2

>>=

右移且赋值运算符

C >>= 2 等同于 C = C >> 2

&=

按位与且赋值运算符

C &= 2 等同于 C = C & 2

^=

按位异或且赋值运算符

C ^= 2 等同于 C = C ^ 2

|=

按位或且赋值运算符

C |= 2 等同于 C = C | 2

 

C#中的其它运算符:

sizeof:它返回的是数据类型的大小

typeof:返回的是class类型

&:取地址符,返回变量的地址

*:指针,指向一个变量

?: 这个是一个三目运算符,比如 condition?x:y;这个表过式表示condition为真的时候,表达式返回x,否则返回y

 

C#中变量的优先级:

对于优先级作了解,不用死记,在不记得的情况下使用()来控制想要的运算优先级即可

类别 

运算符 

结合性 

后缀 

() [] -> . ++ - -  

从左到右 

一元 

+ - ! ~ ++ - - (type)* & sizeof 

从右到左 

乘除 

* / % 

从左到右 

加减 

+ - 

从左到右 

移位 

<< >> 

从左到右 

关系 

< <= > >= 

从左到右 

相等 

== != 

从左到右 

位与 AND 

从左到右 

位异或 XOR 

从左到右 

位或 OR 

从左到右 

逻辑与 AND 

&& 

从左到右 

逻辑或 OR 

|| 

从左到右 

条件 

?: 

从右到左 

赋值 

= += -= *= /= %=>>= <<= &= ^= |= 

从右到左 

逗号 

从左到右 

 

猜你喜欢

转载自blog.csdn.net/advent86/article/details/81713859