从头开始——C#入门

1.变量

using System;

namespace lesson2_变量
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("变量");
            #region 如何声明变量

            //1.有符号的整形变量  是能存储 一定范围  正负数包括0的变量类型

            // sbyte -128到127
            // int -21亿到21亿多
            // short -32768到32767
            // long -9百万兆到9百万兆

            //2.无符号的整形变量 是能存储 一定范围 0和正数的变量类型

            // byte 0到255 
            // uint 0到42亿多
            // ushort 0到65535
            // ulong 0到18百万兆

            //3.浮点数(小数)// 从非零数开始从左到右算有效数字,多的四舍五入

            // float 存储7或8位有效数字  根据编译器不同 有效数字也可能不一样
            // 在C#中 申明的小数 默认是double类型 加f是告诉系统 它是float类型
            // double 存储15到17位有效数字
            // decimal 存储27到28位的有效数字
            // 在C#中 申明的小数 默认是double类型 加m是告诉系统 它是decimal类型

            //4.特殊类型

            // bool true false 表示真假的数据类型
            // char 用来存储单个字符的变量类型(赋值的时候要用单引号括起来 例如char a = '唐';)
            // string 字符串类型 用来存储多个字符 没有上限
            #endregion 

            #region 为什么有那么多不同的变量类型

            #endregion
        }
    }
}

2.变量本质

using System;

namespace lesson3_变量本质
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 变量的存储空间
            // 通过 sizeof 方法 可以获取变量类型所占的内存空间(单位:字节)
            // 有符号
            int sbyteSize = sizeof(sbyte);
            Console.WriteLine("sbyte 所占字节数位:" + sbyteSize);
            int intSize = sizeof(int);
            Console.WriteLine("int 所占字节数位:" + intSize);
            int shortSize = sizeof(short);
            Console.WriteLine("short 所占字节数位:" + shortSize);
            int longSize = sizeof(long);
            Console.WriteLine("sbyte 所占字节数位:" + longSize);
            Console.WriteLine("***************************************");

            // 无符号
            int byteSize = sizeof(byte);
            Console.WriteLine("byte 所占字节数位:" + byteSize);
            int uintSize = sizeof(uint);
            Console.WriteLine("uint 所占字节数位:" + uintSize);
            int ushortSize = sizeof(ushort);
            Console.WriteLine("ushort 所占字节数位:" + ushortSize);
            int ulongSize = sizeof(ulong);
            Console.WriteLine("ulong 所占字节数位:" + ulongSize);
            Console.WriteLine("***************************************");

            // 浮点数
            int floatSize = sizeof(float);
            Console.WriteLine("float 所占字节数位:" + floatSize);
            int doubleSize = sizeof(double);
            Console.WriteLine("double 所占字节数位:" + doubleSize);
            int decimalSize = sizeof(decimal);
            Console.WriteLine("decimal 所占字节数位:" + decimalSize);
            Console.WriteLine("***************************************");

            // 特殊类型
            int boolSize = sizeof(bool);
            Console.WriteLine("bool 所占字节数位:" + boolSize);
            int charSize = sizeof(char);
            Console.WriteLine("char 所占字节数位:" + charSize);
            // sizeof 是不能够得到 string 类型所占的内存大小的
            // 因为字符串长度是可变的 不定
            Console.WriteLine("***************************************");
            #endregion
        }
    }
}

3.变量的命名规则

using System;

namespace lesson4_变量的命名规则
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("变量的命名规则");
            #region 必须遵守的规则
            //1.不能重名
            //2.不能以数字开头
            //3.不能使用程序关键字命名
            //4.不能有特殊符号(下划线除外)

            //建议的命名规则:变量名要有含义->用英文(拼音)表示变量的作用
            //非常不建议的命名规则:用汉字命名
            #endregion
            #region 常用命名规则
            //驼峰命名法 首字母小写 之后每个词的首字母都大写(变量)
            //帕斯卡命名法 所有单词首字母都大写(函数,类)
            //潜在知识点:C#中对大小写是敏感的 是区分的
            #endregion
        }
    }
}

4.常量

using System;

namespace lesson5_常量
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("常量");
            #region 常量的声明
            //关键字 const
            //固定写法:
            //const 变量类型 变量名=初始值;
            //变量的声明
            int i = 20;
            //常量的声明
            const int i2 = 20;
            #endregion

            #region 常量的特点
            //1.必须初始化
            //2.不能被修改
            const float PI = 3.1415926f;
            #endregion
        }
    }
}

5.转义字符

using System;

namespace lesson6_转义字符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("转义字符");
            #region 转义字符的使用
            //什么是转义字符?
            //它是字符串的一部分 用来表示一些特殊含义的字符
            //比如:在字符串当中表现 单引号 引号 空行 。。。
            // string str="123"45";
            #region 固定写法
            //固定写法 \字符
            //不同的 \ 和字符的组合 表示不同的含义

            //常用转义字符
            //单引号 \'
            string str = "\'哈哈哈\'";
            Console.WriteLine(str);
            //双引号 \"
            str = "\"哈哈哈\"";
            Console.WriteLine(str);
            //换行 \n
            str = "123456789\n123456789";
            Console.WriteLine(str);
            //斜杠 \\  
            str = "哈\\哈\\哈";
            Console.WriteLine(str);

            //不常用转义字符(了解)
            //制表符 \t(空一个tab键)
            str = "哈\t哈哈";
            Console.WriteLine(str);
            //光标退格 \b
            str = "123\b123";
            Console.WriteLine(str);
            //空字符 \0
            str = "123\0123";
            Console.WriteLine(str);
            //警报音 \a
            str = "\a";
            Console.WriteLine(str);
            #endregion
            #endregion
            #region 取消转义字符
            string str2 = @"哈哈\哈哈";
            Console.WriteLine(str2);
            Console.WriteLine(@"123\123");
            #endregion
        }
    }
}

6.类型转换_隐式转换

using System;

namespace lesson7_类型转换_隐式转换_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("类型转换-隐式转换");
            //什么是类型转换
            //类型转换就是不同变量类型之间的相互转换
            //隐式转换的基本规则->不同类型之间自动转换
            //大范围装小范围
            #region 相同大类型之间的转换
           
            //有符号 long ->int ->short ->sbyte
            long l = 1;
            int i = 1;
            short s = 1;
            sbyte sb = 1;
            //隐式转换 int隐式转换成了long
            //可以用大范围 装小范围 类型 (隐式转换)
            l = i;
            //不能够用小范围类型去装在大范围的类型
            //i = l;
            l = i;
            l = s;
            l = sb;
            i = s;

            //无符号 ulong ->uint ->ushort ->byte
            ulong ul = 1;
            uint ui = 1;
            ushort us = 1;
            byte b = 1;

            //浮点数 decimal double ->float
            decimal de = 1.1m;
            double d = 1.1;
            float f = 1.1f;
            //decimal这个类型 没有办法用隐式转换的形式 去存储 double和float
            //de=d;
            //de=f;
            //float 是可以影视转换成 double
            d = f;

            //特殊类型 bool char string
            //他们之间 不存在隐式转换

            #endregion

            #region 不同大类型之间的转换

            #region 无符号和有符号之间
            
            //无符号  不能装负数的
            byte b2 = 1;
            ushort us2 = 1;
            uint ui2 = 1;
            ulong ul2 = 1;
            //有符号
            sbyte sb2 = 1;
            short s2 = 1;
            int i2 = 1;
            long l2 = 1;

            //无符号装有符号
            //有符号的变量 不能隐式转换成 无符号的
            //b2 = sb2;
            //us2 = sb2;
            //ul2=sb2;

            //有符号装无符号
            //有符号变量 是可以装 无符号号变量的  前提是 范围一定是要涵盖的 存在隐式转换
            //i2 = ui2;//因为 有符号的变量 可能会超过 这个无符号变量的范围
            i2 = b2;//因为 有符号的变量 不管是多少 都在 无符号变量的范围

            #endregion
            #region 浮点数和整数(有,无符号)之间
            
            //浮点数转整数
            float f2 = 1.1f;
            double d2 = 1.1;
            decimal de2 = 1.1m;
            //浮点数 是可以装载 任何类型 整数的
            f2 = l2;
            f2 = i2;
            f2 = s2;
            f2 = sb2;

            f2 = ul2;
            f2 = ui2;
            f2 = us2;
            f2 = b2;

            f2 = 1000000000000000000;
            Console.WriteLine(f2);

            //decimal 不能隐式存储 float和double
            //但是它可以隐式的存储整形
            de = l2;
            de = ul2;
            //double->float->所有整形(无符号,有符号)
            //decimal->所有整形(无符号,有符号)

            //整数转浮点数  整数是不能隐式存储浮点数的  因为整数不能存小数
            //i2 = f2;
            #endregion
            #region 特殊类型和其他类型之间

            //bool bool没有办法和其他类型 相互隐式转换
            bool bo2 = true;
            char c2 = 'A';
            string str2= "1231";
            //bo2 = i2;
            //bo2 = ui2;
            //bo2 = f2;

            //i2 = bo2;
            //ui2 = bo2;
            //f2 = bo2;

            //bo2 = c2;
            //c2 = bo2;
            //bo2 = str2;
            //str2 = bo2;

            //char char没有办法隐式存储 其他类型的变量
            //char类型 可以隐式转换成 整形和浮点型
            //对应的数字 其实是一个 ASSCII码
            //计算机里面存储 2进制
            //字符 中文 英文 标点符号 在计算机中都是一个数字
            //一个字符 对应一个数字 ASSCII码就是一种对应关系
            i2 = c2;
            Console.WriteLine(c2);
            f2 = c2;
            Console.WriteLine(c2);
            ui2 = c2;
            Console.WriteLine(c2);

            //string string无法和其他类型进行隐式转换

            #endregion
            #endregion

            //总结
            //高精度(大范围)装低精度(小范围)
            //double->float->整数(无符号,有符号)->char
            //decimal->整数(无符号,有符号)->char
            //string和bool 不参与隐式转换规则的

        }
    }
}

7.类型转换_显示转换

using System;

namespace lesson8_类型转换_显示转换_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("类型转换-显示转换");
            #region 括号强转
            //作用 一般情况下 将高精度的类型强制转换位低精度
            //语法:变量类型 变量名 =(变量类型)变量;
            //注意:精度问题 范围问题

            //相同大类的整形
            //有符号整形
            sbyte sb = 1;
            short s = 1;
            int i = 1;
            long l = 1;
            //强转的时候 可能会出现范围问题 造成的异常
            s = (short)i;
            Console.WriteLine(s);
            //无符号整形
            byte b = 1;
            ushort us = 1;
            uint ui = 1;
            ulong ul = 1;
            b = (byte)ui;
            Console.WriteLine(b);

            //无符号和有符号
            uint ui2 = 1;
            int i2 = 1;
            //在强转的时候一定要注意范围 不然得到的结果 可能有异常
            ui2 = (uint)i2;
            Console.WriteLine(ui2);

            i2 = (int)ui2;

            //浮点和整形  浮点数 强转成 整形时 会直接抛弃小数点后面的小数
            i2 =(int) 1.24f;
            Console.WriteLine(i2);

            //char和数值类型
            i2 = 'A';

            char c = (char)i2;
            Console.WriteLine(c);

            //bool和string  是不能通过 括号强转的
            bool bo = true;
            //int i3 = (bool)bo;

            string str="123";
            //i3 = (int)str;

            #endregion

            #region Parse法
            //作用 把字符串类型转换为对应的类型
            //语法:变量类型.Parse("字符串");
            //注意:字符串必须能够转换成对应类型 否则报错

            //有符号
            string str2 = "123";
            int i4 = int.Parse("123");
            Console.WriteLine(i4);
            //我们填写字符串 必须是要能够转成对应类型的字符 如果不符合规则 会报错
            //i4 = int.Parse("123.45");
            //Console.WriteLine(i4);

            //值的范围 必须是能够被变量存储的值 否则报错
            short s3 = short.Parse("4000");
            Console.WriteLine(s3);

            sbyte sb3 = sbyte.Parse("1");
            Console.WriteLine(s3);
            //他们的意思是相同的
            Console.WriteLine(sbyte.Parse("1"));
            Console.WriteLine(long.Parse("123123"));

            //无符号
            Console.WriteLine(byte.Parse("1"));
            Console.WriteLine(ushort.Parse("1"));
            Console.WriteLine(uint.Parse("1"));
            Console.WriteLine(ulong.Parse("1"));

            //浮点数
            float f3 = float.Parse("1.2323");
            double d3 = double.Parse("1.2323");

            //特殊类型
            bool b5 = bool.Parse("true");
            Console.WriteLine(b5);

            char c2 = char.Parse("A");
            Console.WriteLine(c);

            #endregion

            #region Convert法
            //作用 更准确的将 各个类型之间进行相互转换
            //语法:Convert.To目标类型(变量或常量)
            //注意:填写的变量或常量必须正确 否则出错

            //转字符串  如果是把字符串转对应类型 那字符串一定要合法合规
            int a = Convert.ToInt32("12");
            Console.WriteLine(a);

            //精度更准确
            //精度比括号强转好一点 会四舍五入
            a = Convert.ToInt32(1.23456f);
            Console.WriteLine(a);

            //特殊类型转换
            //把bool类型也可以转成 数值类型 true对应1 false对应0
            a = Convert.ToInt32(true);
            Console.WriteLine(a);
            a = Convert.ToInt32(false);
            Console.WriteLine(a);

            a = Convert.ToChar("A");
            Console.WriteLine(a);

            //每一个类型都存在对应的 Convert中的方法
            sbyte sb5 = Convert.ToSByte("1");
            short s5 = Convert.ToInt16("1");
            int i5 = Convert.ToInt32("1");
            long l5 = Convert.ToInt64("1");

            byte b6 = Convert.ToByte("1");
            ushort us6 = Convert.ToUInt16("1");
            uint ui6 = Convert.ToUInt32("1");
            ulong ul6 = Convert.ToUInt64("1");

            float f5 = Convert.ToSingle("12.3");
            double d5 = Convert.ToDouble("13.2");
            decimal de5 = Convert.ToDecimal("13.2");

            bool bo5 = Convert.ToBoolean("true");
            char c5 = Convert.ToChar("A"); 
            string str5 = Convert.ToString("123123");

            #endregion

            #region 其他类型转string
            //作用 拼接打印
            //语法:变量.Tostring();

            string str6 = 1.ToString();
            str6 = true.ToString();
            str6 = "A".ToString();
            str6 = 1.2f.ToString();

            int aa = 1;
            str6 = aa.ToString();
            bool bo6 = true;
            str6 = bo6.ToString();

            //当我们进行字符串拼接时 就会自动调用 Tostring 转成 string
            Console.WriteLine("123123" + 1 + true);

            str6 = "123123" + 1 + true + 1.23;

            #endregion
        }
    }
}

8.异常捕获

using System;

namespace lesson9_异常捕获
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("异常捕获");
            #region 作用
            //将玩家输入的内容 存储 string类型的变量(容器)中
            //string str = Console.ReadLine();
            //Parse转字符串为 数值类型时 必须 要合法合规
            //int i = int.Parse(str);

            //通过对异常捕获的学习 可以避免当代码报错时 造成程序卡死的情况

            #endregion

            #region 基本语法
            //必备部分
            try
            {
                //希望进行异常捕获的代码块
                //放到try中
                //如果try中的代码 报错了 不会让程序卡死
            }
            catch
            {
                //如果出错了 会执行 catch中的代码 来捕获异常
                //catch(Exception e)具体报错跟踪 通过e得到 具体的错误信息
            }
            //可选部分
            finally
            { 
                //最后执行的代码 不管有没有出错 都会执行其中的代码
                //目前不用写
            }
            //异常捕获代码基本结构中 不需要加;在里面写代码逻辑时 才加;

            #endregion

            #region 实践
            try
            {
                string str = Console.ReadLine();
                int i = int.Parse(str);
                Console.WriteLine(i);

            }
            catch
            {
                Console.WriteLine("请输入合法数字!");
            }
            finally
            {
                Console.WriteLine("执行完毕");
            }

            #endregion
        }
    }
}

9.算术运算符

using System;

namespace lesson10_算术运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("算术运算符");
            //算术运算符 是用于 数值类型变量计算的运算符
            //它的返回结果是数值
            #region 赋值符号
            //=
            //关键知识点:
            //先看右侧 再看左侧 把右侧的值赋给左侧的变量

            string myName = "于双";
            int myAge = 21;
            float myHeight = 170.5f;

            #endregion

            #region 加 +
            //用自己计算 先算右侧结果 再赋值给左侧变量
            int i = 1;
            i = i + 2;
            Console.WriteLine(i);

            //连续运算 先算右侧结果 再赋值给左侧变量
            i = 1 + i + i;
            Console.WriteLine(i);

            //初始化时就运算 先算右侧结果 再赋值给左侧变量
            int i2 = 1 + 2 + 3;
            Console.WriteLine(i2);

            #endregion

            #region 减 -
            //用自己计算 先算右侧结果 再赋值给左侧变量
            int j = 1;
            j = j - 1;
            Console.WriteLine(j);
            //连续运算 先算右侧结果 再赋值给左侧变量
            j = 1 - 2 - 3;
            Console.WriteLine(j);
            //初始化时就运算 先算右侧结果 再赋值给左侧变量
            int j2 = 3 - 2 - 1;
            Console.WriteLine(j2);

            #endregion

            #region 乘 *
            //用自己计算 先算右侧结果 再赋值给左侧变量
            int c = 1;
            c = c * 10;
            Console.WriteLine(c);
            //连续运算 先算右侧结果 再赋值给左侧变量
            c = 1 * 2 * 3;
            Console.WriteLine(c);
            //初始化时就运算 先算右侧结果 再赋值给左侧变量
            int c2 = 1 * 2 * 3;
            Console.WriteLine(c2);
            #endregion

            #region 除 /
            //用自己计算 先算右侧结果 再赋值给左侧变量
            int chu = 1;
            chu = 10 / 2;
            Console.WriteLine(chu);
            //连续运算 先算右侧结果 再赋值给左侧变量
            chu = 20 / 2 / 5;
            Console.WriteLine(chu);
            //初始化时就运算 先算右侧结果 再赋值给左侧变量
            int chu2 = 20 / 5 / 2;
            Console.WriteLine(chu2);

            #endregion

            #region 取余 %
            int y = 4;
            y = y % 2;
            Console.WriteLine(y);

            y = 20;
            y = y % 5 % 3;
            Console.WriteLine(y);
            #endregion

            #region 算术运算符的优先级
            //优先级 是指 在混合运算时的运算顺序

            //乘除取余 优先级高于 加减 先算乘除 后算加减

            //括号可以改变优先级 优先计算括号内内容

            //多组括号 先算最里层括号 依次往外算

            #endregion

            #region 算术运算符的 复合运算符
            //固定写法
            //+= -= *= /= %=
            //复合运算符 是用于 自己=自己进行运算

            //注意:复合运算符 只能进行一种运算 不能混合运算
            //例如 i *-+=2;

            #endregion

            #region 算术运算符的 自增减
            int a2 = 1;
            
            a2 = a2 + 1;
            a2 += 1;
            a2++;//先用再加
            Console.WriteLine(a2);
            ++a2;//先加再用
            Console.WriteLine(a2);

            a2 = 1;
            Console.WriteLine(a2++);// 1
            a2 = 1;
            Console.WriteLine(++a2);// 2

            #endregion
        }
    }
}

10.字符串拼接

using System;

namespace lesson11_字符串拼接
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("字符串拼接");
            #region 字符串拼接方式一
            //之前的算术运算符 只是用来数值类型变量进行数学运算的
            //而 string 不存在算术运算符不能计算 但是可以通过+号来进行字符串拼接
            string str = "123";
            //用+号进行字符串拼接
            str = str + "456";
            Console.WriteLine(str);

            str = str + 1;
            Console.WriteLine(str);

            //复合运算符 +=
            str = "123";
            str += "1" + 4 + true;
            Console.WriteLine(str);//12314True

             str += 1 + 2 + 3 + 4;
            Console.WriteLine(str);//12314True10

            str += ""+1 + 2 + 3 + 4;
            Console.WriteLine(str);//12314True101234

            str = "";
            str += 1 + 2 +""+ 3 + 4;
            Console.WriteLine(str);//334

            str = "";
            str += 1 + 2 + "" +( 3 + 4);
            Console.WriteLine(str);//37

            //注意:用+号拼接 是用符号唯一方法 不能用-*/%...

            #endregion

            #region 字符串拼接方式二
            //固定语法
            //string.Format("待拼接的内容",内容1,内容2....);
            //拼接内容中的固定规则
            //想要被拼接的内容用占位符替代{数字} 数字:0到n  依次往后
            string str2=string.Format("我是{0},我今年{1}岁,我想要{2}","于双","21","好好学习,天天向上!");
            Console.WriteLine(str2);

            str2 = string.Format("{0}{1}{2}", 1, true, false);
            Console.WriteLine(str2);

            #endregion

            #region 控制台打印拼接
            Console.WriteLine("A{0}B{1}C{2}", 1, true, false);
            //内容比占位符多没事,比占位符少会报错
            #endregion
        }
    }
}

11.条件运算符

using System;

namespace lesson12_条件运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("条件运算符");
            #region 条件运算符
            //作用:用于比较两个变量或常量
            //是否大于 >
            //是否小于 <
            //是否等于 ==
            //是否不等于 !=
            //是否大于等于 >=
            //是否小于等于 <=

            //条件运算符 一定存在左右两边的内容
            //左边内容 条件运算符 右边内容
            int a = 5;
            int b = 10;
            //条件运算符 不能直接这么用
            //纯比较不用结果 那么对于我们来说 没有任何的意义
            //a>b;
            //比较的结果 返回的是一个 bool 类型的值
            //true 和 false 如果比较的条件满足 那就返回 true 不满足 就返回 false
            //先算右边 再赋值给左边
            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);

            #endregion

            #region 各种应用写法
            //变量和变量比较
            a = 5;
            b = 10;
            result = a < b;
            //变量和数值(常量)比较
            result = a < 10;
            //数值和数值比较
            result = 5 < 10;
            result = a == b;
            result = a != b;
            //计算结果比较
            //条件运算符 优先级 低于算术运算符
            result = a+3 >a-2+3;

            #endregion

            #region 不能进行范围比较
            a = 5;
            //判断是否在某两个值之间
            //1<a<6
            //不能这么写

            #endregion

            #region 不同类型之间的比较
            //不同数值类型之间 可以随意进行条件运算符比较
            int i = 5;
            float f = 1.2f;
            double d = 12.4;
            short s = 2;
            byte by = 20;
            uint ui = 222;
            // 只要是数值 就能够进行条件运算符比较 比较大于小于等于等等

            //特殊类型 char string bool 只能同类型进行 == 和 != 比较
            string str = "123";
            char c = 'A';
            bool bo = true;

            //char 不仅可以和自己类型进行 == != 还可以和数值类型进行比较
            //还可以和 字符类型 进行大小比较
            result = c > 123;
            result = c > 'B';

            #endregion
        }
    }
}

12.逻辑运算符

using System;

namespace lesson13_逻辑运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("逻辑运算符");
            //对 bool 类型 进行逻辑运算
            #region 逻辑与
            //符号 && 并且
            //规则 对两个bool值进行逻辑运算 有假则假 同真为真

            bool result = true && false;
            Console.WriteLine(result);
            result = true && true;
            Console.WriteLine(result);
            result = false && true;
            Console.WriteLine(result);

            //bool相关的类型 bool变量 条件运算符
            //逻辑运算符优先级 低于 条件运算符 低于 算术运算符
            int i= 3;
            result = i > 1 && i < 5;
            Console.WriteLine(result);

            #endregion

            #region 逻辑或
            //符号 || 或者
            //规则 对两个bool值进行逻辑运算 有真则真 同假为假
            result = true || false;
            Console.WriteLine(result);
            result = true || true;
            Console.WriteLine(result);
            result = false || false;
            Console.WriteLine(result);

            #endregion

            #region 逻辑非
            //符号 ! 取反
            //规则 对两个bool值进行取反 真变假 假变真
            result = !true;
            Console.WriteLine(result);
            result = !false;
            Console.WriteLine(result);

            result = !!true;
            Console.WriteLine(result);
            //逻辑非的 优先级 较高  
            //result = !3 < 2;
            result = !(3 < 2);
            Console.WriteLine(result);

            #endregion

            #region 混合使用优先级问题
            //规则 !(逻辑非)优先级最高 &&(逻辑与)优先级高于||(逻辑或)
            //逻辑运算符优先级 低于 算术运算符 条件运算符 (逻辑非除外)


            #endregion

            #region 逻辑运算符短路规则
            int i3 = 1;
            // || 有真则真
            // 只要 逻辑与 或者 逻辑或 左边满足条件
            // i3>0 true
            // 只要 满足条件 右边的内容 对于我们来说 已经不重要
            result = i3 > 0 || ++i3>= 1;
            Console.WriteLine(i3);
            Console.WriteLine(result);
            //false && i3++ >1;抛弃后面不去计算

            #endregion
        }
    }
}

13.位运算符

using System;

namespace lesson14_位运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("位运算符");
            //位运算符 主要用数值类型进行计算的
            //将数值转换为2进制 再进行位运算

            #region 位与 &
            //规则 连接两个数值进行位运算 将数值转换为2进制
            //对位运算 有0则0
            int a = 1;//1
            int b = 5;//101
            // 001
            //&101
            // 001=1
            int c = a & b;
            Console.WriteLine(c);//1

            a = 3;    //00011
            b = 19;   //10011
            c = a & b;//00011=3
            Console.WriteLine(c);//3


            #endregion

            #region 位或 |
            //规则 连接两个数值进行位运算 将数值转换为2进制
            //对位运算 有1则1
            int a2 = 1;//1
            int b2 = 5;//101
            //   001
            // | 101
            //   101=5
            int c2 = a2 | b2;
            Console.WriteLine(c2);//5

            a2 = 3;       //   00011
            b2 = 19;      // | 10011
            c2 = a2 | b2; //   10011=19
            Console.WriteLine(c2);//19

            #endregion

            #region 异或 ^
            //规则 连接两个数值进行位运算 将数值转换为2进制
            //对位运算 相同为0 不同为1
            int a3 = 1;//1
            int b3 = 5;//101
            //   001
            // ^ 101
            //   100=4
            int c3 = a3 ^ b3;
            Console.WriteLine(c3);//4

            a3 = 3;       //   00011
            b3 = 19;      // ^ 10011
            c3 = a3 ^ b3; //   10000=16
            Console.WriteLine(c3);//16

            #endregion

            #region 位取反 ~
            //规则 连接两个数值进行位运算 将数值转换为2进制
            //对位运算 0变1 1变0
            int a4 = 5;    
            //  0000 0000 0000 0000 0000 0000 0000 0101
            // ~1111 1111 1111 1111 1111 1111 1111 1010
            // 反码补码知识
            int c4 = ~a4;
            Console.WriteLine(c4);//-6

            #endregion

            #region 左移<< 和 右移 >>
            //规则 让一个数的2进制进行左移和右移
            //左移几位 右侧加几个0
            a = 5;//101
            c = a << 5;//10100000=160
            Console.WriteLine(c);//160

            //右移几位 右侧去掉几个数
            a = 5;//101
            c = a >> 2;//1
            Console.WriteLine(c);//1

            #endregion
        }
    }
}

14.三目运算符

using System;

namespace lesson15_三目运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("三目运算符");

            #region 基本语法
            //套路 3个空位 2个符号!!!
            //固定语法 空位     ? 空位                 :空位;
            //关键信息 bool类型 ? bool类型为真返回内容 :bool内容为假返回内容;
            //三目运算符 会有返回值,这个返回值类型必须一致,并且必须使用!

            #endregion

            #region 具体使用

            string str =true ? "条件为真" : "条件为假";
            Console.WriteLine(str);
            string str2 = false ? "条件为真" : "条件为假";
            Console.WriteLine(str2);

            int a = 5;
            str = a < 1 ? "条件为真" : "条件为假";
            Console.WriteLine(str);

            //第一个空位 始终是结果为bool类型的表达式 bool变量 条件表达式 逻辑运算符表达式
            //第二三个空位 什么表达式都可以 只要保证他们的结果类型是一致的

            #endregion
        }
    }
}

15.条件分支语句if

using System;

namespace lesson16_条件分支语句if
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("条件分支语句 if");
            #region 作用
            //让顺序执行的代码 产生分支
            //if语句是第一个 可以让我们的程序 产生逻辑变化的语句

            #endregion

            #region if语句
            //作用 满足条件时 多执行一些代码
            //语法:
            //if(bool类型值)
            //{
            //   满足条件要执行的代码 写在if代码块中;
            //}
            //注意
            //1.if语句的语法部分,不需要写分号
            //2.if语句可以嵌套使用

            #endregion

            #region if……else语句

            #endregion

            #region if……else if……else语句

            #endregion
        }
    }
}

16.条件分支语句_switch

using System;

namespace lesson17_条件分支语句_switch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("switch 语句");

            #region 作用
            //让顺序执行的代码 产生分支

            #endregion

            #region 基本语法
            /*switch (变量)
            {
                case 常量
                    满足条件执行的代码逻辑;
                    break;
                case 常量
                    满足条件执行的代码逻辑;
                    break;
                default:
                    如果上面case的条件都不满足 就会执行 default中的代码
                        break;
            }*/
            //注意:常量!!!只能写一个值  不能写一个范围 不能写条件运算符 逻辑运算符
            // switch 值判断变量是否等于某一个固定值!!!

            #endregion

            #region default 可省略

            #endregion

            #region 可自定义常量

            #endregion

            #region 贯穿
            //作用 满足某些条件时 做的事情是一样的 就可以使用贯穿
            int aa = 1;
            switch (aa)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                    // case 和 break 之间可以写n句语句
                    //并且可以嵌套使用
                    Console.WriteLine("是个数字");
                    Console.WriteLine("是个数字");
                    Console.WriteLine("是个数字");
                    Console.WriteLine("是个数字");
                    break;
            }
                //不写case后面配对的 break 就叫做贯穿
                //满足其中一个条件就会执行后面的代码
         
            #endregion
        }
    }
}

17.循环语句while

using System;

namespace lesson18_循环语句while
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("循环语句-while");
            #region 作用
            //让顺序执行的代码 可以不停的循环执行某一代码块的内容
            //条件分支语句 是 让代码产生分支
            //循环语句 是 让代码可以被重复执行

            #endregion

            #region 语法相关
            /*while (bool类型的值) 
            {
                //当满足条件时 就会执行while语句块中的内容
                //…………
                //当代码逻辑执行完 会回到while循环开头
                //再次进行条件判断
            } */

            //死循环
            //就不停的执行循环中的逻辑“直到死为止”
            //1.可能因为内存问题 造成程序 崩溃闪退
            //2.造成程序卡死
            //
            //
            /* while (true) 
            {
                Console.WriteLine("************");
            }*/
            int c = 1;
            while (c < 10)
            {
                c++;
            }
            Console.WriteLine(c);

            #endregion

            #region 嵌套使用
            int a = 0;
            int b = 0;
            while (a < 10)
            {
                ++a;
                while (b < 10)
                {
                    ++b;
                }
            }
            Console.WriteLine(a+" "+ b);
            #endregion

            #region 流程控制关键词
            //作用     :控制循环逻辑的关键词
            //break    :跳出循环
            while (true)
            {
                Console.WriteLine("break之前的代码");
                break;
                Console.WriteLine("break之后的代码");
            }
            Console.WriteLine("循环外的代码");

            int i = 0;
            while (true)
            {
                ++i;
                Console.WriteLine(i);
                if (i == 10)
                {
                    break;
                }
            }
            Console.WriteLine(i);

            //continue :回到循环开始 继续执行
            /* while (true)
             {
                 Console.WriteLine("continue之前的代码");
                 continue;
                 Console.WriteLine("continue之后的代码");
             }
             Console.WriteLine("循环外的代码");*/

            //打印 1 到 20 之间的 奇数

            /* i = 0;
             while (i < 20)
             {
                 i++;
                 if (i%2=1)
                 {
                     Console.WriteLine(i);
                 }
             }*/

            i = 0;
            while (i < 20)
            {
                i++;
                if (i % 2 == 0)
                {
                    continue;
                }
                Console.WriteLine(i);
            }

            //注意:break 和 continue 主要是和循环配合使用的 和 if语句无关
            //break 在 switch 中的作用 和 while 循环中的作用有异曲同工之妙

            #endregion
        }
    }
}

18.循环语句do_while

using System;

namespace lesson19_循环语句do_while
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("do……while循环");
            #region 基本语法
            //while循环       是先判断条件再执行
            //do……while循环 是先执行一次循环语句块中的逻辑 再判断是否继续
            /*do
            {
                do while 循环语句块;
            } while (bool类型的值);*/

            #endregion

            #region 实际使用
            /*do
            {
                Console.WriteLine("do while 循环语句块");
            } while (true);*/

            #endregion

            #region 嵌套使用
            //在do……while 里面的 continue 会回到 while 里面

            #endregion
        }
    }
}

19.循环语句_for

using System;

namespace lesson20_循环语句_for
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("循环语句-for");
            #region 基本语法
            /*for (初始表达式; 条件表达式; 增量表达式)
            {
                循环代码逻辑;
            }*/
            //第一个空(初始表达式):一般声明一个临时变量,用来计数
            //第二个空(条件表达式):表明进入循环的条件 一个bool类型的结果(bool变量 条件运算符 逻辑运算符)
            //第三个空(增量表达式):用第一个空中变量 进行 自增减运算

            /* for (int i = 0; i < 10; i++)
             {
                 Console.WriteLine(i);
                 //执行完循环语句块中的逻辑后
                 //最后执行第三个空中的代码
             }*/

            #endregion

            #region 支持嵌套
            /*for (int i = 0; i < 10; i++)
            {
                for (int j = 1;j<10;j++)
                {
                    Console.WriteLine(j);
                }
            }*/

            #endregion

            #region 特殊写法
            //三个空位 可以都空着 按需求去写
            //for循环可以写死循环
            for (; ; )
            {
                Console.WriteLine("for循环的死循环");
            }
            #endregion

            #region 对比while循环
            //for循环 一般用来可以准确得到 一个范围中的所有数
            

            #endregion
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值