09 运算符和表达式(2)

9. 条件逻辑运算符

逻辑与 AND-&&, 逻辑或OR-|| 运算符是二元左结合运算。逻辑非NOT-!是一元运算符。

Expr1 && Expr2 

 Expr1 || Expr2

!Expr1

测试代码:

bool b1 = true;

bool b2 = false;

bool b3 = true;

Console.WriteLine(b1 &&b2); //False

Console.WriteLine(b1 && b3); //True

Console.WriteLine(b1||b2); //True

Console.WriteLine(b1 || b3); //True

Console.WriteLine(!b1); //False

Console.WriteLine(!b2); //True

条件逻辑运算使用“短路”模式操作,意思是如果计算Expr1之后结果已经确认,就会跳过Expr2

bool bv;

int iv = 9;

bv = (1 == 2) && (9 == ++iv);

Console.WriteLine(bv);

Console.WriteLine(iv);

输出:

False

9


 bv = (1 == 1) && (9 == ++iv);

Console.WriteLine(bv);

Console.WriteLine(iv);

输出:

False

10

从上测试代码看,Expr1不能确认结果,才会执行Expr2 


10. 逻辑运算符

按位逻辑运算:位与 &, 位或 |,位异或^, 位非 !

int a = 12; //0000 1100

int b = 10; //0000 1010

Console.WriteLine(a&b);//0000 1000 =>8

Console.WriteLine(a|b);//0000 1110 =>14

Console.WriteLine(a^b);//0000 0110 =>6

Console.WriteLine(~a); //1111 0011 =>-13


11. 移位运算

按位移运算符将位组向左或向右移动指定数目个位置,空出的位置用0或者1填充。

Operand << Count //左移 ,左移1位,结果为它乘以2

OPerand >> Count //右移, 右移1位,结果为它除以2

测试代码:

int x, y, z = 14; // 0000 1110

x = z << 3; //左移3位 0111 1000 -->112

y = z >> 3; //右移3位 0000 0001 -->1

Console.WriteLine(x);

Console.WriteLine(y);

11. 赋值运算符

对运算符右边的表达式求值,并应该值设置运算符左边的变量表示的值。

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

复合赋值

x = x + (y-z); 等价 x += y-z;


12.条件运算符

条件运算符是一种简洁的方法,基于条件的结果,返回2个值之一。 ?:

condition ? Expression1:Expression2

测试代码:

int x = 1, y = 2;

int v = 0;

 if (x < y)

     v = 5;

 else

    v = 10;

Console.WriteLine(v);//5

条件运算修改上代码:

v = x < y ? 5 : 10;

Console.WriteLine(v);  //5


13. 一元算术运算符

一元算术运算符设置数字值得符合:

+ ,一元正运算符简单返回操作数的值

-, 一元负运算符返回0减操作数的值

int x = +10;

int y = -x;

 int z = -y;

Console.WriteLine($"{x} {y} {z}"); //10, -10, 10


14. 用户定的类型转换

隐式转换:在特定的上下文中,使用特定类型时,如有必要,编译器会自动执行行转换。

显示转换:编译器只在使用显示转换运算符是才执行转换

强制转换运算符:使用一对圆括号,其中放置目标类型,如 (int)value;


15.运算符的重载

运算符重载只能用于结构和类。

一元运算符的重载方法带一个独立的class或struct类型参数。

二元运算符的重载方法带2个参数,其中至少有一个必须是class或者struct类型。


运算符重载的方法声明需要:

必须同时使用static, public修饰符

运算符必须要操作的类或结构的成员。


16. typeof 运算符

返回作为其参数的任何类型的System.Type对象。通过该对象可以了解类型特征。

测试代码:


using System;

using System.Reflection;

namespace _06_Typeof运算符

{

    class Program

    {

        class demo

        {

            public int Field1;

            public int Field2;

            public void Method1() { }

            public int Method2() { return 1; }

        }

        static void Main(string[] args)

        {

            Type t = typeof(demo);

            FieldInfo[] fi = t.GetFields();

            MemberInfo[] mi = t.GetMethods();

            foreach (FieldInfo f in fi)

            {

                Console.WriteLine($"Field:{f.Name}");

            }

            foreach (MemberInfo m in mi)

            {

                Console.WriteLine($"Method:{m.Name}");

            }

        }

    }

}

输出:

Field:Field1

Field:Field2

Method:Method1

Method:Method2

Method:Equals

Method:GetHashCode

Method:GetType

Method:ToString


使用GetType方法:

demo d = new demo();

Console.WriteLine($"Type is :{d.GetType().Name}");

输出:demo


17. nameof运算符

返回一个表示传入参数的字符串

string s = "Local variable";

 Console.WriteLine(nameof(s));

Console.WriteLine(nameof(demo));

Console.WriteLine(nameof(demo.Method1));

Console.WriteLine(nameof(demo.Field1));

Console.WriteLine(nameof(demo.Method2));

 Console.WriteLine(nameof(demo.Field2));

输出:

s

demo

Method1

Field1

Method2

Field2


nameof只返回其参数的非限定名称。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容