c#运算符

算数运算符

// 算术运算符 +,-,*,/,%,++,--

/*int a = 10;
int b = 20;*/

// int a = 10, b = 20;

/*int a, b;
a = 10;
b = 20;*/
int a, b;
a = b = 20; // 赋值语句,从右向左运算
int c = a + b;
Console.WriteLine(c);

// ctrl+k+d快速格式化代码。
Console.WriteLine(30 - 10);
Console.WriteLine(30 * 10);  // * 乘法
Console.WriteLine(10 / 3);   // 3,默认类型是Int32
Console.WriteLine(Convert.GetTypeCode(10 / 3));

double f = 10 / 3; // 除法要想得到浮点数,必须明确确定变量类型
Console.WriteLine(f);
Console.WriteLine(Convert.GetTypeCode(f));

// % 取模,取余
Console.WriteLine(10 % 3);
Console.WriteLine(5 % 3);
Console.WriteLine(5 % 5);

// ++ 累加运算符,自增运算符
// 规律:++在前,先加1,再使用。++在后,先使用,后加1。
int a1 = 1;
/*
Console.WriteLine(a1++); // 1
Console.WriteLine(a1);
*/

/*
Console.WriteLine(++a1); // 2
Console.WriteLine(a1); // 2
*/

/*
int b2 = a1++; // b2=1  a1=2
Console.WriteLine($"{b2},{a1}");
*/

/*
a1++; // a1 = a1 + 1;
Console.WriteLine(a1); // 2
*/

/*
++a1; // a1 = a1 + 1;
Console.WriteLine(a1);
*/

// -- 累减运算符,自减运算符
// 规律:--在前,先减1,后使用。--在后,先使用,后减1。


int a2 = 1;
a2++;  // a2=2
Console.WriteLine(a2--); // 2, a2=1
Console.WriteLine(--a2); // 0, a2=0
Console.WriteLine(a2++); // 0, a2=1
Console.WriteLine(++a2); // 2, a2=2

int a3 = 1;
a3 = a3 + 2; // 一次加2,必须重新赋值,只有a3 = a3 + 1才可以缩写成a3++或++a3。


// 关系运算符  ==, !=, >, <, >=, <=

int A = 10;
int B = 20;
Console.WriteLine(A == B); // Flase
Console.WriteLine(A != B); // True
Console.WriteLine(A > B);  // Flase
Console.WriteLine(A < B);  // True
Console.WriteLine(A >= B); // Flase
Console.WriteLine(A <= B); // True

// ASCII码即(America Standard Code for Information Interchange)美国信息交换标准码
// http://www.zzbaike.com/wiki/ASCII
// utf-8万国码  unicode编码
// https://blog.csdn.net/weixin_50464560/article/details/119157294
char C = 'A';
char D = 'B';
Console.WriteLine(C > D); // False
Console.WriteLine('a' > 'A'); // 97 > 65  True

Console.WriteLine(true == true);
Console.WriteLine(true != true);
Console.WriteLine(false == false);
Console.WriteLine(false != false);

逻辑运算符

// &&逻辑与运算符,简称与  and 并列  规律:所有的操作数为true,则为true。
// ||逻辑或运算符,简称或  or  或者  规律:任何一个操作数为true,则为true。
// !逻辑非运算符, 简称非  not 取反  规律:取反

bool a = true;
bool b = false;
bool c = true;
Console.WriteLine(a && b && c); // False
Console.WriteLine(a || b || c); // True
Console.WriteLine(!a); // False
Console.WriteLine(!b); // True

// Console.WriteLine(10 && 11); // C#不支持 强调:C#中的逻辑运算符的操作数必须是布尔值,不能是""字符串,数字0,非0,null

// 其他语言中,如javascript, "",0,null都会默认隐式转换false,但在C#没有此特性。
string str = "";
if (str == string.Empty) // 不能if(str){}这样写
{
    Console.WriteLine("C#中空字符串不能直接隐式转换成False");
}

int num = 0;
if (num == 0) // 不能if(num){}这样写
{
    Console.WriteLine("C#中0不能直接隐式转换成False");
}

object? obj = null;
if (obj == null) // 不能if(obj){}这样写
{
    Console.WriteLine("C#中null不能直接隐式转换成False");

}

其他运算符

namespace ConsoleApp7
{
    class Program
    {
        public static void Main(string[] args)
        {
            // See https://aka.ms/new-console-template for more information
            Console.WriteLine("Hello, World!");
            // 其他运算符

            int a = 10;
            int a1 = 11;
            Console.WriteLine(a);

            // 三目运算符,三元运算符,拥有三部分,第一部分是条件表达式,返回布尔值。
            // 第二部分是第一个操作数,条件表达式为真,返回第二个操作数
            // 第三部分是第二个操作数,条件表达式为假,返回第三个操作数。
            // 细节:问号和冒号是英文的符号
            // 三目运算符相当于if...else...语句
            //int b = a == 10 ? 20 : 30;
            /*
            int b = ((a == 10 && a1==11) ? 20 : 30);
            Console.WriteLine(b); 
            */

            // int b = ((a == 10 && a1==11) ? 20 : 30);相当于如下的代码
            int b = 0;
            if (a == 10 && a1 == 11)
            {
                b = 20;
            }
            else
            {
                b = 30;
            }
            Console.WriteLine(b);

            // typeof() 和is关键字,用来判断类型
            /*
            int c = 20;
            Console.WriteLine(typeof(c)); // typeof()不能判断变量
            */

            Person p = new Person(); // p是个实例,不是类型
            Console.WriteLine(typeof(Person));  // ConsoleApp7.Person
            Console.WriteLine(typeof(Int32));   // System.Int32
            Console.WriteLine(typeof(String));  // Stytem.String

            Console.WriteLine(Convert.GetTypeCode(p)); // Object
            Console.WriteLine(Convert.GetTypeCode(2));  // Int32
            Console.WriteLine(Convert.GetTypeCode("hello")); // String


            // 子类Person的实例p肯定是Person类类型。Person类型称为类,类是一种数据类型,开发者自定义的数据类型。
            Console.WriteLine(p is Person); // True
            // 由于Person继承于Animal,所以Person的实例p也是父类Animal的类型
            Console.WriteLine(p is Animal); // True

            // is一般用来判断引用类型,不建议判断值类型。
            Console.WriteLine(10 is Int32); // True

            Console.WriteLine("abc" is String); // True

            double d = 20.12;
            int e = (int)d; // 强制转换的语法,这种强制转换会失败,抛异常。
            Console.WriteLine(e);

            //int f = d as int;

            Animal animal = p as Animal; // as 强制转换语法,但不论是否转换成功,不抛异常

        }
    }
    class Animal { }

    class Person:Animal { }

    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉玊则不达

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值