异常捕获

使用异常捕获可以捕获出现异常的代码块,防止因为异常抛出造成的程序卡死的情况发生。

try{}catch{}finally{}结构

//异常捕获
try
{
string str=Console.ReadLine();
int i=int.Parse(str);
Console.WriteLine("输入的字符串数值转为int的数值"+i);
}catch
{
Console.WriteLine("请输入合法数字");
}finally
{
//无论正常执行还是是否进行异常捕获 都会执行
Console.WriteLine("执行完毕");
}

运算符

算术运算符

算术运算符是英语数值类型变量运算的运算符,运算结果仍旧为数值。

赋值运算符:=

注意:赋值运算符理解将右边数值赋值给左边变量。

算术运算符:

//算术数运算符
//加+
int i1=5;
int sum=1+2+3+i1*2;
//减 -
int j1=6;
int sub=15-j1-2;
//乘 *
int c=5;
int c1=5*c;
//除 /
int d=28;
int d1=d/4;
//取模 (取余) %
int e=12;
e=e%5;
Console.WriteLine(e);

算术运算符的优先级

乘除取余 > 加减

int a = 1 + 2 * 3 / 2 + 1 + 2 * 3; //=11
Console.WriteLine(a); a = 1 + 4 % 2 * 3 / 2 + 1; //2
Console.WriteLine(a); //括号可以改变优先级 优先计算括号内内容
a = 1 + 4 % (2 * 3 / 2) + 1; //3
Console.WriteLine(a); //多组括号 先算最里层括号 依次往外算
a = 1 + (4 % (2 * (3 / 2))) + 1; //2
Console.WriteLine(a);

复合运算符

+= -= *= /= %= 相当于对自身进行某项算术操作之后的结果重新赋给自己

//符合运算符
int i3 = 1;
i3 = i3 + 2;
Console.WriteLine(i3); i3 = 1;
i3 += 2;//i3 = i3 + 2;
Console.WriteLine(i3); i3 = 2;
i3 += 2;//4
i3 -= 2;//2
i3 /= 2;//1
i3 *= 2;//2
i3 %= 2;//0
Console.WriteLine(i3); int i4 = 10;
// i4 += 4
i4 += 20 * 2 / 10;
Console.WriteLine(i4); //注意:复合运算符 只能进行一种运算 不能混合运算
//i4 */-= 2;

自增/减运算符

注意理解前置还是后置的运算符,区别先用后自增/减还是先自增/减再使用。

可以理解为电击小子打怪物,小光先变身成电击小子打怪兽还是打完怪兽再变身。

//自增运算符
//自增运算符 让自己+1
a2 = 1;
a2++;//先用再加
Console.WriteLine(a2);
++a2;//先加再用
Console.WriteLine(a2);
a2 = 1;
Console.WriteLine(a2++);//1
//2
Console.WriteLine(++a2);//3 //自减运算符 让自己-1
a2 = 1;
a2--;//先用再减
--a2;//先减再用 a2 = 1;
Console.WriteLine(a2--);//1
//0
Console.WriteLine(--a2);//-1
//思考?这个
int a = 10, b = 20;
// 11 + 20
int number1 = ++a + b;
Console.WriteLine(number1);//31
a = 10;
b = 20;
//10 + 20
int number2 = a + b++;
Console.WriteLine(number2);//30
a = 10;
b = 20;
//10 + 21 + 11
int number3 = a++ + ++b + a++;
Console.WriteLine(number3);//42
Console.WriteLine(a);//12

字符串的拼接

  1. 使用+拼接

    string str = "123";
    //用+号进行字符串拼接
    str = str + "456";
    Console.WriteLine(str);
    str = str + 1;
    Console.WriteLine(str);
    //使用+=
    str = "123";
    str += "1" + 4 + true;
    Console.WriteLine(str); //注意:只要遇到字符串,就是转为字符串
    string str = "畅知";
    str += 1 + 2 + 3 + 4;
    Console.WriteLine(str);//畅知10 str = "";
    str += "" + 1 + 2 + 3 + 4;//开头就变为字符串
    Console.WriteLine(str);//1234 str = "";
    str += 1 + 2 + "" + (3 + 4);//先算括号,从首到尾计算
    Console.WriteLine(str);//37 str = "123";
    str = str + (1 + 2 + 3);
    Console.WriteLine(str);//1236
  2. 使用占位符替换方式拼接(占位符从0开始,用{}括起来)

    string str2 = string.Format("我是{0}, 我今年{1}岁, 爱好:{2}", "畅知", 21, "我爱写博客!!");
    Console.WriteLine(str2); str2 = string.Format("asdf{0},{1},sdfasdf{2}", 1, true, false);
    Console.WriteLine(str2);

条件运算符

条件运算符均为双目运算符,返回结果为bool类型的,使用其运算结果来做某些情况的判断。

条件运算符的优先级要低于算术运算符

//条件运算符
int a = 5;
int b = 10;
//大于
bool result = a > b;
Console.WriteLine(result);
//小于
result = a < b;
Console.WriteLine(result);
//大于等于
result = a >= b;
Console.WriteLine(result);
//小于等于
result = a <= b;
Console.WriteLine(result);
//等于
result = a == b;
Console.WriteLine(result);
//不等于
result = a != b;
Console.WriteLine(result);
//也可以直接和数值比较
result=10>5; //优先级
// 先计算 再比较
result = a + 3 > a - 2 + 3;// true
result = 3 + 3 < 5 - 1;//false //不同类型之间的比较
//不同数值类型之间 可以随意进行条件运算符比较
int i = 5;
float f = 1.2f;
double d = 12.4;
short s = 2;
byte by = 20;
uint ui = 666; //只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
int i = 5;
float f = 1.2f;
double d = 12.4;
short s = 2;
byte by = 20;
uint ui = 666;
bool result;
//只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
result = i > f;//true
Console.WriteLine(result);
result = f < d;//true
Console.WriteLine(result);
result = i > by;//false
Console.WriteLine(result);
result = f > ui;//false
Console.WriteLine(result);
result = ui > d;//true
Console.WriteLine(result); //特殊类型 char string bool 只能同类型进行 == 和 != 比较
string str = "123";
char c = 'A';
bool bo = true; result = str == "234";//false
result = str == "123";//true
result = str != "123";//false result = c == 'B';//false //不仅可以和自己类型进行 == != 还可以和数值类型进行比较
//字符参与比较大小时候将自身作为ASCII码比较
//还可以和字符类型进行大小比较
result = c > 123;
result = c > 'B'; result = bo == true;//true;

逻辑运算符

逻辑与 & 逻辑或 || 逻辑非 !

逻辑运算符优先级 < 条件运算符 算术运算

条件运算符均为双目运算符,返回结果为bool类型的,使用其运算结果来做某些情况的判断。

条件运算符的优先级要低于算术运算符

//条件运算符
int a = 5;
int b = 10;
//大于
bool result = a > b;
Console.WriteLine(result);
//小于
result = a < b;
Console.WriteLine(result);
//大于等于
result = a >= b;
Console.WriteLine(result);
//小于等于
result = a <= b;
Console.WriteLine(result);
//等于
result = a == b;
Console.WriteLine(result);
//不等于
result = a != b;
Console.WriteLine(result);
//也可以直接和数值比较
result=10>5; //优先级
// 先计算 再比较
result = a + 3 > a - 2 + 3;// true
result = 3 + 3 < 5 - 1;//false //不同类型之间的比较
//不同数值类型之间 可以随意进行条件运算符比较
int i = 5;
float f = 1.2f;
double d = 12.4;
short s = 2;
byte by = 20;
uint ui = 666; //只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
int i = 5;
float f = 1.2f;
double d = 12.4;
short s = 2;
byte by = 20;
uint ui = 666;
bool result;
//只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
result = i > f;//true
Console.WriteLine(result);
result = f < d;//true
Console.WriteLine(result);
result = i > by;//false
Console.WriteLine(result);
result = f > ui;//false
Console.WriteLine(result);
result = ui > d;//true
Console.WriteLine(result); //特殊类型 char string bool 只能同类型进行 == 和 != 比较
string str = "123";
char c = 'A';
bool bo = true; result = str == "234";//false
result = str == "123";//true
result = str != "123";//false result = c == 'B';//false //不仅可以和自己类型进行 == != 还可以和数值类型进行比较
//字符参与比较大小时候将自身作为ASCII码比较
//还可以和字符类型进行大小比较
result = c > 123;
result = c > 'B'; result = bo == true;//true;

逻辑运算符

逻辑与 & 逻辑或 || 逻辑非 !

逻辑运算符优先级 < 条件运算符 算术运算

逻辑运算符中: !(逻辑非)优先级最高 &&(逻辑与)优先级高于||(逻辑或)

//逻辑运算符
//逻辑与&& 并且
//规则: 对两个bool值进行逻辑运算 有假则假 同真为真
bool result = true && false;
Console.WriteLine(result);
result = true && true;
Console.WriteLine(result);
result = false && true;
Console.WriteLine(result); //bool相关的类型 bool变量 条件运算符
//逻辑运算符优先级 低于 条件运算符 算术运算
// true && true
result = 3 > 1 && 1 < 2;
Console.WriteLine(result);
int i = 3;
// 1 < i < 5;
// true && true
result = i > 1 && i < 5;
Console.WriteLine(result); //多个逻辑与 组合运用
int i2 = 5;
// true && false && true && true
//在没有括号的情况下 从左到右 依次看即可
//有括号 先看括号内
result = i2 > 1 && i2 < 5 && i > 1 && i < 5;
Console.WriteLine(result);
//符号 || 或者
//规则 对两个bool值进行逻辑运算 有真则真 同假为假
result = true || false;
Console.WriteLine(result);
result = true || true;
Console.WriteLine(result);
result = false || true;
Console.WriteLine(result);
result = false || false;
Console.WriteLine(result);
// false || true
result = 3 > 10 || 3 < 5;
Console.WriteLine(result);//true int a = 5;
int b = 11;
// true || true || false
result = a > 1 || b < 20 || a > 5;
Console.WriteLine(result);
// ? && ?
// ? || ?
// ? 可以是写死的bool变量 或者 bool值
// 还可以是 条件运算符相关 //----------逻辑非!
//符号 !
//规则 对一个bool值进行取反 真变假 假变真 result = !true;
Console.WriteLine(result);
result = !false;
Console.WriteLine(result);
result = !!true;
Console.WriteLine(result);
//逻辑非的 优先级 较高
result = !(3 > 2);
Console.WriteLine(result); a = 5;
result = !(a > 5);
Console.WriteLine(result); //混合使用逻辑运算符的优先级问题
// 规则 !(逻辑非)优先级最高 &&(逻辑与)优先级高于||(逻辑或)
// 逻辑运算符优先级 低于 算数运算符 条件运算符(逻辑非除外) bool gameOver = false;
int hp = 100;
bool isDead = false;
bool isMustOver = true; //false || false && true || true;
//false || false || true;
result = gameOver || hp < 0 && !isDead || isMustOver;
Console.WriteLine(result);

逻辑运算符的短路原则(聪明的运算符)

//短路原则
//|| 判断原则为有真则真 所以第一个条件判断为真,则直接可以得出运算结果为真 便不会检查第二个
//个运算条件的真假
//同理,&& 若左边条件为假,则不会判断右边条件,直接可以得出运算结果为假
//逻辑或 有真则真 那左边只要为真了 右边就不重要
int i3=1;
bool result = i3 > 0 || ++i3 >= 1;
Console.WriteLine(i3);//1
Console.WriteLine(result);
// false && i3 ++ > 1;抛弃后面不去计算 //逻辑与 有假则假 那左边只要为假了 右边就不重要
result = i3 < 0 && i3++ > 1;
Console.WriteLine(i3);//1
Console.WriteLine(result); //思考?
//求打印结果是什么?
//注意运算符的优先级
bool gameOver;
bool isWin;
int health = 100;
gameOver = true;
isWin = false;
// true || false && true
Console.Write(gameOver || isWin && health > 0);

位运算符

位运算是基于二进制编码的运算,首先将值转换为二进制数值,然后对于位进行操作运算。

运算符:位与& 位或| 异或^ 位或 | 位取反! 左移<< 右移>>

//位运算符
//位与& 有0则0 全1才1
// 对位运算 有0则0
int a = 1;// 001
int b = 5;// 101
// 001
//& 101
// 001 = 1
int c = a & b;
Console.WriteLine(c); //多个数值进行位运算 没有括号时 从左到右 依次计算
a = 1;// 001
b = 5;// 101
c = 19;//10011
// 00001
//& 00101
// 00001
//& 10011
// 00001
int d = a & b & c;
Console.WriteLine(d); //位或| 有1则1,全0则0
a = 1;//001
b = 3;//011
c = a | b;
// 001
//| 011
// 011
Console.WriteLine(c); a = 5; // 101
b = 10;// 1010
c = 20;//10100
// 00101
//| 01010
// 01111
//| 10100
// 11111 => 1 + 2 + 4 + 8 + 16 =31 Console.WriteLine(a | b | c); //异或^ =======
//不同为1 相同为0
// 对位运算 相同为0 不同为1
a = 1; //001
b = 5; //101
// 001
//^101
// 100
c = a ^ b;
Console.WriteLine(c); a = 10; // 1010
b = 11; // 1011
c = 4; // 100
// 1010
//^ 1011
// 0001
//^ 0100
// 0101 = 5
Console.WriteLine(a ^ b ^ c); //位取反 ~ 取反
// 对位运算 0变1 1变0
a = 5;
// 0000 0000 0000 0000 0000 0000 0000 0101
// 1111 1111 1111 1111 1111 1111 1111 1010
// 反码补码知识
// 计算机中的二进制是以补码形式存储的
//补码:正数的补码是本身 负数的补码是绝对值取反加一
c = ~a;
Console.WriteLine(c); //左移 右移
// 规则 让一个数的2进制数进行左移和右移
// 左移几位 右侧加几个0
a = 5; // 101
c = a << 5;
// 1位 1010
// 2位 10100
// 3位 101000
// 4位 1010000
// 5位 10100000 = 32 + 128 = 160
Console.WriteLine(c); // 右移几位 右侧去掉几个数
a = 5; // 101
c = a >> 2;
// 1位 10
// 2位 1
Console.WriteLine(c);
//练习----
//99 ^ 33 和 76 | 85 的结果为? // 1100011 ^ 100001
// 1100011
//^0100001
// 1000010
Console.WriteLine(99 ^ 33); // 1001100 | 1010101
// 1001100
//|1010101
// 1011101 => 64 + 29 = 93
Console.WriteLine(76 | 85);

三目运算符

条件?A :B 条件为真则走A逻辑否则走B

//三目运算符
int a = 5;
str = a < 1 ? "a大于1" : "a不满条件";
Console.WriteLine(str);
int i = a > 1 ? 123 : 234;
//第一个空位 始终是结果为bool类型的表达式 bool变量 条件表达式 逻辑运算符表达式
//第二三个空位 什么表达式都可以 只要保证他们的结果类型是一致的
bool b = a > 1 ? a > 6 : !false;

======

我会每天更新C#学习笔记,感兴趣的可以给个订阅!

点个订阅,练习C#代码不迷路!

C#学习笔记---异常捕获和变量的更多相关文章

  1. 关于SQLServer2005的学习笔记—异常捕获及处理

    转自:http://blog.csdn.net/baoqiangwang/article/details/5395874 SQLServer2005 提供了类似于 C# 和 C++ 语言中的异常处理的 ...

  2. JavaScript学习笔记——JS中的变量复制、参数传递和作用域链

    今天在看书的过程中,又发现了自己目前对Javascript存在的一个知识模糊点:JS的作用域链,所以就通过查资料看书对作用域链相关的内容进行了学习.今天学习笔记主要有这样几个关键字:变量.参数传递.执 ...

  3. C#.NET学习笔记7--11---算术运算符,变量赋值,变量的交换,布尔表达式1,布尔表达式2

    C#.NET学习笔记7---算术运算符 2013/9/6 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com 1.Consol ...

  4. JavaScript学习笔记(八)——变量的作用域与解构赋值

    在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...

  5. python学习笔记——异常

    转自 http://www.cnblogs.com/rubylouvre/archive/2011/06/22/2086644.html Python内建异常体系结构 BaseException +- ...

  6. C++学习笔记--异常简介

    C++异常是对程序运行过程中发生的异常情况(如被0除)的一种响应.异常提供了将控制权从程序的一个部分传递到另一部分的途径. 1.对异常的处理有3个部分组成: (1)引发异常 (2)捕获有处理程序的异常 ...

  7. go 学习笔记之有意思的变量和不安分的常量

    首先希望学习 Go 语言的爱好者至少拥有其他语言的编程经验,如果是完全零基础的小白用户,本教程可能并不适合阅读或尝试阅读看看,系列笔记的目标是站在其他语言的角度学习新的语言,理解 Go 语言,进而写出 ...

  8. 0016 Java学习笔记-异常-如果try-catch-finally中都存在return语句会怎样?

    上午在搜索"System.runFinalization"的时候,搜到 http://www.cnblogs.com/Skyar/p/5962253.html ,其中有关于try- ...

  9. Java学习笔记--异常描述

    异常描述 1.简介 为了全面了解"异常"的概念,先来分析一个实例.假定要编写一个Java程序,该程序读取用户输入的一行文本,并在终端显示该文本.这里是一个演示Java语言I/O功能 ...

  10. Java学习笔记18---final关键字修饰变量、方法及类

    英语里final这个单词大家都知道是"最终的"意思,其实还有一个意思是"不可更改的".在Java里,final关键字作"不可更改的"来解释更 ...

随机推荐

  1. 05-面试必会-SpringBoot&SpringCloud

    01- 讲一讲 SpringBoot 自动装配的原理 1.在 SpringBoot 项目的启动引导类上都有一个注解@SpringBootApplication 这个注解是一个复合注解, 其中有三个注解 ...

  2. TheTransformerPlaybookforNLPandLanguageUnderstanding

    目录 2.1 基本概念解释 2.2 技术原理介绍 2.3 相关技术比较 3. 实现步骤与流程 3.1 准备工作:环境配置与依赖安装 3.1.1 准备工作:环境配置与依赖安装 3.1.2 核心模块实现 ...

  3. typescript的必要性及使用

    1 前言 作为一个前端语言,Javascript从最初只是用来写页面,到如今的移动终端.后端服务.神经网络等等,它变得几乎无处不在.如此广阔的应用领域,对语言的安全性.健壮性以及可维护性都有了更高的要 ...

  4. 1.6 编写双管道ShellCode后门

    本文将介绍如何将CMD绑定到双向管道上,这是一种常用的黑客反弹技巧,可以让用户在命令行界面下与其他程序进行交互,我们将从创建管道.启动进程.传输数据等方面对这个功能进行详细讲解.此外,本文还将通过使用 ...

  5. influxdb常用sql总结

    本文为博主原创,转载请注明出处: 1.登录influxdb influx -username admin -password "password" 2.查看数据库 ##查看有哪些数 ...

  6. vue报错解决Duplicate keys detected: ‘[object Object]’

    最近在做vue项目时遇到了报错 ​ Duplicate keys detected: '[object Object]'. This may cause an update error. ​ 由于这个 ...

  7. 从ABNF读懂HTTP协议格式

    定义 HTTP(Hyper Text Transfer Protocol)超文本传输协议 HTML( Hyper Text Markup Language)超文本标记语言 URI(Uniform Re ...

  8. Mybatis开发中的常用Maven配置

    Mybatis导入Maven配置 <!-- MyBatis导入 --> <dependency> <groupId>org.mybatis</groupId& ...

  9. Protobuf vs JSON

    Protobuf(Protocol Buffers)和 JSON 都是数据序列化格式,但它们在许多方面有着显著的不同.以下是对两者的一些主要比较: 数据大小和速度: Protobuf:由于 Proto ...

  10. Spark RDD惰性计算的自主优化

    原创/朱季谦 RDD(弹性分布式数据集)中的数据就如final定义一般,只可读而无法修改,若要对RDD进行转换或操作,那就需要创建一个新的RDD来保存结果.故而就需要用到转换和行动的算子. Spark ...