c# 个人笔记 正在学习中....

  • vs快捷键
    在这里插入图片描述
  • 折叠冗余的使用
    在这里插入图片描述
    在这里插入图片描述

hello word

在这里插入图片描述

数据类型

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

占位符的所有

  //占位符的使用
            String name = "小明";
            int age = 18;
            String gender = "男";
            long telephone = 13428888815;
            decimal wages = 10000;
            Console.WriteLine("您好,我叫{0},今年{1}岁了,性别:{2},电话:{4},工资{3}",name,age,gender,wages,telephone);
            Console.ReadKey();

在这里插入图片描述

c#中的@

在这里插入图片描述

转义符

在这里插入图片描述

类型转换

  • 强制类型转换 (大的转小的)
    在这里插入图片描述

  • 隐式类型转换 int转double
    在这里插入图片描述

  • c#中占位符输入指定几位小数
    在这里插入图片描述

Convert类型转换

当需要将字符串的数字"123" 转为 int 或 double 123 时 需要使用Conver来转换

在这里插入图片描述

  • 字符串数字转int

将字符串数字转int 使用Convert.ToInt32();时 但 是"123abc"含有字符时会出现报错 可以使用 int.TryParse(); 方法

在这里插入图片描述

异常捕获

在这里插入图片描述

  • 觉得测试
    在这里插入图片描述

流程控制

c#中的switch

//返回今天是星期几
            int weekDate = (int)DateTime.Now.DayOfWeek; 
            string week = "不晓得是星期几";

            switch (weekDate)
            {
                case 0:
                    week = "星期天";
                    break;
                case 1:
                    week = "星期一";
                    break;
                case 2:
                    week = "星期二";
                    break;
                case 3:
                    week = "星期三";
                    break;
                case 4:
                    week = "星期四";
                    break;
                case 5:
                    week = "星期五";
                    break;
                case 6:
                    week = "星期六";
                    break;
                default:
                    week = "啥也不是";
                    break;
            }

            Console.WriteLine(week);

c#中的循环

  • while
  			//while (循环条件) 为true就执行循环体 为false跳出循环体
            //{
            //    循环体
            //}

            //打印10次hello word
            int i = 0;
            while (i < 10)
            {
                i++;
                Console.WriteLine("第{0}个 hello word",i);
            }


            //登录 用户名admin 密码admin
            string name = "";
            string pas = "";
            Console.WriteLine("请输入用户名");
            name = Console.ReadLine();
            Console.WriteLine("请输入密码");
            pas = Console.ReadLine();
            while (name != "admin" || pas != "admin")
            {
                Console.WriteLine("用户名或密码输入错误请重新输入!!!");
                Console.WriteLine("请输入用户名");
                name = Console.ReadLine();
                Console.WriteLine("请输入密码");
                pas = Console.ReadLine();

            }
            Console.WriteLine("登录成功");
            Console.ReadKey();

在这里插入图片描述

Continue 关键字

continue 立即结束本次循环,回到循环条件,如果成立,则继续进入下一次循环 (continue 后面的代码不执行)

在这里插入图片描述

//计算1-100之间能被7整除的数的合
            int i = 0;
            int num = 0;
           while  (i <= 100)
            {
                i++;
                if ((i % 7) == 0)
                {
                    continue;
                }
                num += i;
            }
            Console.WriteLine(num);
            Console.ReadKey();

for 循环

  • 99乘法口诀表
  for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= 9; j++)
                {
                    Console.Write("{0}*{1}={2}\t", i, j, i * j);
                }
                Console.WriteLine(); // 输出空 换行
            }
            Console.WriteLine("=============================================");
            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("{0}*{1}={2}\t", j, i, i * j);
                }
                Console.WriteLine(); // 输出空 换行
            }

            Console.ReadKey();

c#中随机数 Random

在这里插入图片描述

c#中常量

c#中常量申明也是使用const

	 const int nums = 50; //申明常量 不可重新赋值

C#中的枚举

在这里插入图片描述

namespace study08_meiju
{

    //定义公共的枚举
    public enum Gander
    {,}

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("枚举");

         //变量类型  变量名 =  值 
            Gander  gander = Gander.;  //获取值 通过枚举名.出来
            Console.WriteLine(gander);
            Console.WriteLine(Gander.);

        }
    }
}

在这里插入图片描述

  • 改变枚举的索引 (默认为0)
 public enum Gander
    {
        A = 1,  //索引为1
        B,      //索引为2    
        C = 5,  //索引为5   
        D,     //索引为6    
        E      //索引为7    
    }
  • 枚举的有关类型转换
using System;

namespace study08_meiju
{

    //定义公共的枚举
    public enum Gander
    {,}

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("枚举");

            //变量类型  变量名 =  值 
            Gander  gander = Gander.;  //获取值 通过枚举名.出来
            Console.WriteLine(gander);
            Console.WriteLine(Gander.);

            //枚举类型转int 
            int intt = (int)gander;
            Console.WriteLine("+++++++枚举类型转int++++++++++++++");
            Console.WriteLine(intt); //枚举索引从0开始计算 所以这里输出 0

            //将int类型强转为枚举类型
            Console.WriteLine("+++++++将int类型强转为枚举类型++++++++++++++");
            int intt1 = 1;
            Gander state = (Gander)intt1; 
            Console.WriteLine(state);  //相当于取数组对应索引的值 这里为1 输出女

            //将枚举类型转字符串  (所有类型都可以转字符串类型!!!通过.ToString() )
            string str = gander.ToString();
            Console.WriteLine("------将枚举类型转字符串----");
            Console.WriteLine(str);

            //将字符串0转枚举类型

            string s = "0";

            Gander ganders = (Gander)Enum.Parse(typeof(Gander), s);
            Console.WriteLine("----将字符串0转枚举类型-----");
            Console.WriteLine(ganders);


            Console.ReadKey();



        }
    }
}

在这里插入图片描述

c#结构

结构:帮助我们一次申明多个不同类型变量

//语法
//public struct Name
//{
//成员; 字段
//}

namespace study09_结构
{
    class Program
    {
        //定义一个学生结构
        public struct Person  //命名规范首字母大写
        {
            public string _name;  //字段名前面加_ 下划线
            public int _age;
            public Gender _gender;
        }
        //定义性别的枚举
        public enum Gender
        {,}
        static void Main(string[] args)
        {
            //所有结构
            Person xiaoming;
            xiaoming._name = "小明";
            xiaoming._age = 18;
            xiaoming._gender = Gender.;
            Console.WriteLine("我叫{0},性别{1},今年{2}岁了",xiaoming._name,xiaoming._gender,xiaoming._age);




            Console.ReadKey();

        }
    }
}

在这里插入图片描述

c#中的数组

			//申明一个长度为10的数组 索引0-9 默认值都为0
            int[] nums = new int[10];
            Console.WriteLine("申明一个长度为10的数组 索引0-9 默认值都为0:{0}", nums[0]);
            //申明一个指定长度的数组并赋值 索引0-9 
            int[] numsTow = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Console.WriteLine("申明一个指定长度的数组并赋值 索引0-9 :{0}", numsTow[9]);

在这里插入图片描述

  • 字符串转数组–数组转字符串
  			  string str1 = "路飞,索隆,乌索普,山治,娜美,乔巴,罗宾,费兰奇,甚平";
            string[] strArray = str1.Split(','); //字符串转数组
            Console.WriteLine("字符串转数组:");
            for (int v = 0; v < strArray.Length; v++)
            {
                Console.WriteLine(strArray[v]);
            }

            //数组转成字符串
            string newStr = string.Join(",", strArray);
            Console.WriteLine("数组转成字符串");
            Console.WriteLine(newStr);

在这里插入图片描述

  • 冒泡升序排序
 int[] intAtt = {9,8, 7, 6, 5, 4, 3, 2, 1, 0 };

            for (int a = 0; a < intAtt.Length -1; a++)
            {
                for (int s = 0; s < intAtt.Length -1 -a ; s++)
                {
                    if (intAtt[s] > intAtt[s + 1 ])
                    {
                        int temp = intAtt[s];
                        intAtt[s] = intAtt[s + 1];
                        intAtt[s + 1] = temp;
                    }
                }
            }
            Console.WriteLine("冒泡升序排序");
            for (int b = 0; b < intAtt.Length; b++)
            {
                Console.WriteLine(intAtt[b]);
            }

在这里插入图片描述

c#中array方法实现排序

 int[] intAtt2 = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
            Array.Sort(intAtt2);  //将int类型数组进行升序排列
            Console.WriteLine("将int类型数组进行升序排列");
            for (int d = 0; d < intAtt2.Length; d++)
            {
                Console.WriteLine(intAtt2[d]);
            }
            Array.Reverse(intAtt2); //将int类型数组进行方转
            Console.WriteLine("将int类型数组进行方转");
            for (int d = 0; d < intAtt2.Length; d++)
            {
                Console.WriteLine(intAtt2[d]);
            }

在这里插入图片描述

c#中的函数方法

在这里插入图片描述

  • 语法(当没有返回类型时写void)
        语法
        [public] static 返回类型 方法名([参数])
        {
            方法体;
        }
 		/// <summary>
        /// 求两个数的最大值 返回int类型
        /// </summary>
        /// <param name="num1">数字1</param>
        /// <param name="num2">数字2</param>
        /// <returns>int</returns>
        public static int GetMax(int num1,int num2)
        {
            return num1 > num2 ? num1 : num2;
        }
  • 调用 在Main方法中调用
 static void Main(string[] args)
        {
           Console.WriteLine(Program.GetMax(2, 3));
        }

在这里插入图片描述

c# 方法 out参数

out 参数 (当方法要返回多个类型的变量时 使用out多余参数)

  • 定义
 		/// <summary>
        /// 给一个int数组 返回其最大值 最小值 平均值 
        /// </summary>
        /// <param name="arr">要进行计算的int数组</param>
        /// <param name="max">多余参数最大值</param>
        /// <param name="min">多余参数最小值</param>
        /// <param name="sum">多余参数平均值</param>
        public static void GetNum(int[] arr,out int max,out int min,out int sum)
        {
            // ! ! !out参数必须赋值 !!!
            max = arr[0];
            min = arr[0];
            sum = arr[0];
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] > max)
                {
                    max = arr[i];
                }
                if (arr[i] < min)
                {
                    min = arr[i];
                }
                sum += arr[i];
            }

            sum = sum / arr.Length;

        }
  • 使用
  			int[] arr = { 1, 2, 3, 4, 5, 6 };
            int max;
            int min;
            int sum;
            /// ! ! ! 调用时 out实参必须存在其接收 !!!
            GetNum(arr, out max, out min, out sum);
            Console.WriteLine("最大值是{0}", max);
            Console.WriteLine("最小值是{0}", min);
            Console.WriteLine("平均值是{0}", sum);

在这里插入图片描述

c# 方法 ref参数

ref 能将一个变量代入到方法中进行改变 ,改变后再带出方法 不需要拿变量来接收

namespace study12_方法ref参数
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello study12_方法ref参数!");

            //ref 能将一个变量代入到方法中进行改变 ,改变后再带出方法 不需要拿变量来接收
            /// !!!ref与out不同 out参数必须在方法里赋值 ref必须在方法外调用前赋值 !!!
            int num1 = 10;
            int num2 = 20;
            FN(ref num1, ref num2); //所有方法 在变量前加ref 
            Console.WriteLine("此时num1的值为{0}",num1);
            Console.WriteLine("此时num2的值为{0}", num2);


        }

        public static void FN(ref int num1,ref int num2)
        {
            int temp = num1;
            num1 = num2;
            num2 = temp;
        }
    }
}

在这里插入图片描述

c# 方法 params可变参数

在这里插入图片描述

c# 方法重载

  • 概念:方法的重载指的就是方法名一样,但参数不一样。
  • 参数不同有两种概念
  • (如果参数个数相同,那么参数类型就不能一样)
  • (如果参数类型相同,那么参数的个数就不能一样)
  • 方法的重载与有无返回值无关

c# 类

类包含
字段 - (字段是受保护的通常用private修饰)
属性 - (属性 的作用就是保护字段,对字段的取值和赋值进行限定)
方法

  • 创建一个学生类
 class Person
    {
        //字段 必须是受保护的 !!! 
        // 字段是不能给外界访问的 通过属性来控制字段的值
        private string _name;         //定义的名字的字段 private -》字段修饰符 只能在当前类中访问
        // 属性
        public string Name  //定义名字的属性
        {
            // set 与 get 均可以设置处理值
            // 属性取值 get
            get { return _name; }
            // 属性设值 set 
            set { _name = value; }
        }
        int _age; // 定义年龄的字段
        public int Age //定义年龄的属性
        {
            set
            {
                //在给属性(也就是字段)赋值时 进行判断年龄不能小于0大于100 
                if (value <= 0 || value >= 100)
                {
                    _age = 0;
                }
                _age = value;
            }
            get
            {
                return _age;
            }
        }
         char _gender; //定义性别字段
        public char Gender //定义性别属性
        {
            get
            {
                // 在获取属性(也就是字段)时 进行判断 如果设置属性是不是女 也不是男 默认获取属性时返回男
                 if (_gender != '男' && _gender != '女')
                {
                    return '男';
                }
                return _gender;
            }
            set
            {
                _gender = value;
            }
        }

        public int Ii { get => ii; set => ii = value; }

        private int ii = 123;
        public void Hell()
        {
            Console.WriteLine("我叫{0},我是个小{1}生,我今年{2}岁了", this.Name, this.Gender, this.Age);
        }
    }
  • 创建类的实例 new一个对象
 static void Main(string[] args)
        {
            Person xiaoming = new Person();
            xiaoming.Name = "小明";
            xiaoming.Gender = '男';
            xiaoming.Age = 8;
            xiaoming.Hell();
            Console.ReadKey();
        }

在这里插入图片描述

  • 静态类与非静态类
    在这里插入图片描述

C# 构造函数

C#中构造函数 没有返回值 也不需要viod
构造函数名必须与类名一致
构造函数可以存在重载

//构造函数study
    class Study01
    {
        //申明字段其属性
        private string _name;  
        private int _age;
        private char _gender;
        public string Name { get { return _name; } set { _name = value; } }
        public int Age { get => _age; set => _age = value; }
        public char Gender { get => _gender; set => _gender = value; }

        // C#中构造函数 没有返回值 也不需要viod
        //构造函数名必须与类名一致
        public Study01(string Name,int Age,char Gender)
        {
            this.Name = Name;
            this.Age = Age;
            this.Gender = Gender;
        }
        //构造函数可以存在重载
        public Study01()
        {
            Console.WriteLine("构造函数可以存在重载");
        }
        public void Hell()
        {
            Console.WriteLine("你好我叫{0},今年{1}岁了,是个小{2}生", this.Name, this.Age, this.Gender);
        }
    }
  • 通过this构造函数调用构造函数

this代表当前的对象
在类当中显示的调用本类的构造函数 this:

在这里插入图片描述

c#字符串方法

在这里插入图片描述


string str ="";

string[] strArray = str.Split('截取字符'); //按字符将字符串拆分为数组

str = str.Replace("n","m");//将字符串n替换为m

str = str.Remove(i,length);//删除字符串下标i,长度为8下标从0开始

str =  str.Substring(n); //截取字符串,下标充0开始,包括n

bool bl = string.IsNullOrEmpty(str)//判断str是否为""和nul,返回true为""或者null

bool bl = str.Contains("n"); //判断字符串str里面是否包含"n",返回true包含,false不包含

bool bl = str.Equals(str1);//判断字符串str和字符串str1是否完全一样(区分大小写) 返回true完全一样

int index = str.IndexOf("n");
int index = str.LastIndexOf("n");

//IndexOf 和 LastIndexOf  判断字符串第一次出现(IndexOf)和最后一次出现(LastIndexOf)的位置,如果没有出现过则返回值为-1

string s=str.Insert(index,"")//在字符串的index位置上插入字符,原来的字符依次后移,变成一个新的字符串

char[] s=str.ToCharArray();//ToCharArray将字符串转化为字符数组(<string>.ToCharArray())

str = str.Trim();// 去掉前后两边的空格
str =  str.TrimStart(); //去除前面的空格
str = str.TrimEdn(); //去除后面的空格

str=str.ToUpper();//转换为大写
str=str.ToLower();//转换为小写
//ToUpper(转换为大写)和ToLower(转换为小写)

c#值类型与引用类型

在这里插入图片描述

c#类的继承

在这里插入图片描述

继承的特性:
1、单根性:一个子类只能有一个父类
2、传递性:子类可以继承父类以及父类的父类的公共属性和方法

using System;

namespace 类的继承Study
{
    class Program
    {
        static void Main(string[] args)
        {
            Student obj1 = new Student("小明", 35,'男',"摄影");
            obj1.Hell();
            Driver obj2 = new Driver("小红", 36, '女', 3);
            obj2.Hell();
            Console.ReadKey();
        }
    }

    class Person
    {
        private string _name;

        private int _age;
        private char _gendre;

        public int Age { get => _age; set => _age = value; }
        public char Gendre { get => _gendre; set => _gendre = value; }
        public string Name { get => _name; set => _name = value; }

        public Person(string name,int age,char gender)
        {
            this._name = name;
            this._age = age;
            this._gendre = gender;
        }

       
    }

    class Student : Person //继承Person 类
    {
        private string _hobby;
        public string Hobby { get => _hobby; set => _hobby = value; }

        public Student(string name,int age,char gender,string hobby):base(name,age,gender) //调用父类的构造函数并传递参数
        {
            this._hobby = hobby;
        }

        public void Hell()
        {
            Console.WriteLine("我是名记者 我的爱好是{0},我的年龄是{1}岁,我是一名{2}狗仔",this.Hobby, this.Age,this.Gendre);
        }

    }

    class Driver : Person
    {
        private int _experience;

        public int Experience { get => _experience; set => _experience = value; }
        public Driver(string name,int age,char gender,int experience):base(name,age,gender)
        {
            this.Experience = experience;
        }

        public void Hell()
        {
            Console.WriteLine("我叫{0},我今年{1}岁,我的驾龄是{2}年", this.Name,this.Age,this.Experience);
        }
    }
}

在这里插入图片描述

new 关键字在继承中

  • new的作用1:创建对象
  • new的作用2:隐藏从父类那里继承过来和本类同名的成员

使用场景:当父类和子类的成员同名时 会自动隐藏父类成员这里就需要new了
在这里插入图片描述

c# 里氏转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        //c# 里氏装换
        //1、子类可以赋值给父类
        //2、如果父类中装了子类对象,那么可以说这个父类强转为了子类对象
        static void Main(string[] args)
        {
            //1、子类可以继承给父类
            Person p = new Student();
            //2、如果父类中装了子类对象,那么可以说这个父类强转为了子类对象
            Student s = (Student)p;
            //调用子类的方法
            s.StudentHell();


            //is:表示类型转换,如果能够转换成功,则返回true否则返回false
            if (p is Student)
            {
                Student s2 = (Student)p;
                s2.StudentHell();
            }
            else
            {
                Console.WriteLine("转换失败!");
            }
            //as:表示类型转换,如果能够转换则返回对应的对象,否则返回null
            Teacth s3 = p as Teacth;
            Console.WriteLine(s3);
            Console.ReadKey();
        }

        class Person
        {
            public void PersonHell()
            {
                Console.WriteLine("我是人类");
            }
        }

        class Student :Person
        {
            public void StudentHell()
            {
                Console.WriteLine("我是学生类");
            }
        }
        class Teacth :Person
        {
            public void StudentHell()
            {
                Console.WriteLine("我是老师类");
            }
        }
    }
}

关于强类型与js弱类型

  	 static void Main(string[] args)
        {
            //c#是一名强类型语言:在代码中,必须对每一个变量的类型有一个明确的定义 申明变量前必须申明变量类型 如:int num = 1;
            int num = 1;
            //js是一门弱类型语言 申明变量无需明确变量类型
            // var根据申明变量时赋的值来推导出变量的类型来 在c#中如果用var 申明变量不赋值后那么在改变值的时候就会报错 推导不出变量类型来
            var num2 = 123;
        }

装箱与拆箱

  • 装箱:将值类型转换为引用类型(更耗性能)
  • 拆箱:将引用类型转换为值类型
    看两种类型有没有发生装箱或者拆箱,就看,这两种类型是否存在继承关系

小游戏 飞行棋

using System;
using System.Security.Cryptography.X509Certificates;

namespace study_飞行棋
{
    class Program
    {
        //地图数组
        public static int[] _GameArr = new int[100];
        //玩家坐标
        public static int[] _UserArr = new int[2];
        //玩家名
        public static string[] _GameUserName = { "A", "B" };
        //控制玩家暂停一回合
        public static bool[] _isTrue = new bool[2];
        static void Main(string[] args)
        {

            GameShow(); // 游戏头的显示
            GameInit(); //地图初始化

            GetGameName(); //获取玩家游戏名
            Console.Clear(); //清空
            GameShow(); // 游戏头的显示
            DramMap(); // 画地图
            while (_UserArr[0] < 99 && _UserArr[1] < 99)
            {
                if (_isTrue[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    _isTrue[0] = false;
                }
                if (_isTrue[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    _isTrue[1] = false;
                }
                //有人到终点游戏结束
                if (_UserArr[0] >= 99)
                {
                    Console.WriteLine("游戏结束!玩家{0}已到达终点", _GameUserName[0]);
                    break;
                }
                if (_UserArr[1] >= 99)
                {
                    Console.WriteLine("游戏结束!玩家{0}已到达终点", _GameUserName[1]);
                    break;
                }

            }
            Console.WriteLine("游戏结束!");
            Console.ReadKey();
        }

        /// <summary>
        /// 画游戏头
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("******SkyBlue--飞行棋1.0******");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("******************************");
        }

        /// <summary>
        /// 地图初始化
        /// </summary>
        public static void GameInit()
        {
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 }; //幸运轮盘 ◎

            for (int i = 0; i < luckyturn.Length; i++)
            {
                _GameArr[luckyturn[i]] = 1;
            }
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷 ☆

            for (int j = 0; j < landMine.Length; j++)
            {
                _GameArr[landMine[j]] = 2;
            }
            int[] pause = { 9, 27, 60, 93 };//暂停 ▲
            for (int k = 0; k < pause.Length; k++)
            {
                _GameArr[pause[k]] = 3;
            }
            int[] tiemTunel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道 卐
            for (int l = 0; l < tiemTunel.Length; l++)
            {
                _GameArr[tiemTunel[l]] = 4;
            }
        }

        //画地图
        public static void DramMap()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("游戏图例:幸运轮盘 ◎ | 地雷 ☆ |  暂停 ▲ | 时空隧道 卐 | A {0} | B {1}", _GameUserName[0], _GameUserName[1]);
            #region 第一横行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DramStringMap(i));
            }
            Console.WriteLine();
            #endregion

            #region 第一数列
            for (int j = 30; j < 35; j++)
            {
                for (int k = 0; k < 29; k++)
                {
                    Console.Write("  "); //画空格
                }
                Console.Write(DramStringMap(j));
                Console.WriteLine(); //换行
            }
            #endregion

            #region 画第二横行
            for (int a = 64; a >= 35; a--)
            {
                Console.Write(DramStringMap(a));
            }
            Console.WriteLine("");
            #endregion

            #region  画第二数列
            for (int e = 65; e <= 69; e++)
            {
                Console.WriteLine(DramStringMap(e));
            }
            #endregion

            #region 第三横行
            for (int c = 70; c <= 99; c++)
            {
                Console.Write(DramStringMap(c));
            }
            Console.WriteLine("");
            #endregion

        }


        /// <summary>
        /// 画地图的各图标
        /// </summary>
        /// <param name="i">地图索引</param>
        /// <returns>str 图标</returns>
        public static string DramStringMap(int i)
        {
            string str = "";
            //如果玩家A与玩家B在同一个坐标显示<> 并且在第一行上
            if (_UserArr[0] == _UserArr[1] && _UserArr[1] == i)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                str = "<>";
            }
            else if (_UserArr[0] == i)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                str = "A";
            }
            else if (_UserArr[1] == i)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                str = "B";
            }
            else
            {
                switch (_GameArr[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.DarkMagenta;
                        str = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        str = "卐";
                        break;
                }
            }

            return str;
        }


        public static void GetGameName()
        {
            Console.WriteLine("请输入玩家A的名字");
            _GameUserName[0] = Console.ReadLine();
            while (_GameUserName[0] == "")
            {
                Console.WriteLine("玩家A的名字不能为空!请重新输入:");
                _GameUserName[0] = Console.ReadLine();
            }

            Console.WriteLine("请输入玩家B的名字");
            _GameUserName[1] = Console.ReadLine();
            while (_GameUserName[1] == "" || _GameUserName[0] == _GameUserName[1])
            {
                if (_GameUserName[1] == "")
                {
                    Console.WriteLine("玩家A的名字不能为空!请重新输入:");
                    _GameUserName[1] = Console.ReadLine();
                }
                if (_GameUserName[0] == _GameUserName[1])
                {
                    Console.WriteLine("玩家B的名字不能和玩家A的名字一样!请重新输入:");
                    _GameUserName[1] = Console.ReadLine();
                }

            }
        }

        /// <summary>
        /// 开始投塞子 
        /// </summary>
        /// <param name="userI">投塞子的玩家A或者B</param>
        public static void PlayGame(int userI)
        {
            Random r = new Random();
            int num = r.Next(1, 7); //每次走几步
            Console.WriteLine("{0}按任意键开始掷骰子", _GameUserName[userI]);
            Console.ReadKey(true); //传true 按的任意键的值不显示
            Console.WriteLine("{0}抛出了{1}", _GameUserName[userI], num);
            _UserArr[userI] += num; //玩家移动位置
            MaxAndMin();

            Console.ReadKey(true);
            Console.WriteLine("按任意键{0}开始移动", _GameUserName[userI]);
            Console.ReadKey(true);
            Console.Clear();
            DramMap();
            Console.WriteLine("{0}移动完了", _GameUserName[userI]);

            // 开始判断玩家踩到对应时的处理
            if (_UserArr[userI] == _UserArr[1 - userI])  //如果玩家踩到了另一个玩家
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},{0}退六格", _GameUserName[userI], _GameUserName[1 - userI], _GameUserName[userI]);
                _UserArr[userI] -= 6;
                MaxAndMin();
                Console.Clear();
                DramMap();

            }
            else //踩到了关卡
            {
                switch (_GameArr[_UserArr[userI]])
                {
                    case 0:
                        Console.WriteLine("玩家{0}踩到了方块,安全", _GameUserName[userI]);
                        break;
                    case 1:
                        Console.WriteLine("玩家{0}踩到了幸运轮盘,请输入:1--交换位置 2--轰炸对方", _GameUserName[userI]);
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("玩家{0}选择了1--交换位置", _GameUserName[userI]);
                                Console.ReadKey(true);
                                int sum = 0;
                                sum = _UserArr[userI];
                                _UserArr[userI] = _UserArr[1 - userI];
                                _UserArr[1 - userI] = sum;
                                MaxAndMin();
                                Console.Clear();
                                DramMap();
                                Console.WriteLine("玩家位置交换成功!!!按任意键继续游戏");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择了2--轰炸对方", _GameUserName[userI]);

                                _UserArr[1 - userI] -= 6;
                                MaxAndMin();
                                Console.Clear();
                                DramMap();
                                Console.ReadKey(true);
                                Console.WriteLine("玩家{0}被轰炸了!以退六格", _GameUserName[1 - userI]);
                                Console.WriteLine("按任意键继续游戏");
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能输入输入1或者2 !请重新输入");
                                input = Console.ReadLine();

                            }
                        }
                        break;
                    case 2:
                        Console.WriteLine("玩家{0}踩到了地雷,暂停一回", _GameUserName[userI]);
                        Console.ReadKey(true);
                        _isTrue[userI] = true;
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", _GameUserName[userI]);
                        Console.ReadKey(true);
                        _UserArr[userI] += 10;
                        MaxAndMin();
                        Console.Clear();
                        DramMap();
                        break;

                }
            }


        }

        /// <summary>
        /// 控制玩家不超出地图
        /// </summary>
        public static void MaxAndMin()
        {
            if (_UserArr[0] < 0)
            {
                _UserArr[0] = 0;
            }
            if (_UserArr[0] >= 99)
            {
                _UserArr[0] = 99;
            }

            if (_UserArr[1] < 0)
            {
                _UserArr[1] = 0;
            }
            if (_UserArr[1] >= 99)
            {
                _UserArr[1] = 99;
            }
        }

    }
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

集合ArrayList

 	static void Main(string[] args)
        {
            //创建集合对象
            ArrayList list = new ArrayList();
            //添加单个元素
            list.Add(true);
            list.Add("小明");
            list.Add(18);
            //添加集合对象
            list.AddRange(new int[] { 1, 2, 3, 4, 5 });
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
            Console.ReadKey();
        }
  • 集合小练习
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04集合ArrayList练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建集合,里面添加一些数字,求平均值与求和
            ArrayList list = new ArrayList();
            list.Add(12);
            list.AddRange(new int[] {1,2,3,5,4,8});
            int num = 0;
            for (int j = 0; j < list.Count; j++)
            {
                num += (int)list[j];
            }
            Console.WriteLine("求和为:{0}", num);
            Console.WriteLine("平均值:{0}", num / list.Count);

            //写一个长度为10的集合,要求存随机的数值,不能重复
            ArrayList list2 = new ArrayList();
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                int randomi = random.Next(0, 10);
                if (!list2.Contains(random))
                {
                    list2.Add(randomi);
                }
                else
                {
                    i--;
                }
            }

            Console.WriteLine("++++++++++++++++++++++++++++++++");
            for (int q = 0; q < list2.Count; q++)
            {
                Console.WriteLine(list2[q]);
            }

            Console.ReadKey();
        }
    }
}

在这里插入图片描述

Hashtable 键值对 集合

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _05Hashtable集合
{
    class Program
    {
        static void Main(string[] args)
        {
            //new一个Hashtable 键值对集合
            Hashtable list = new Hashtable();
            list.Add(1, "小明");
            list.Add(2, 18);
            list.Add(3,'男');
            list.Add(false, "唱歌很好听");
            list.Add("delete", "删除我把");
           

            //集合的相关方法

            //判断是否存在键
            if (list.ContainsKey("abc"))
            {
                //通过list[]来赋值时 当abc以存在时会覆盖之前的值 
                list["abc"] = "新添加的元素";

            }
            else
            {
                Console.WriteLine("abc键值已存在");
            }
            //list.Clear() 移除集合中的所有元素; 
            list.Remove("delete"); //删除指定值

			//foreach 循环输出
            foreach (var item in list.Keys)
            {
                Console.WriteLine("键为{0}的值为{1}", item, list[item]);
            }
            Console.WriteLine("------------------黄金分割线-----------------------");
            foreach (var item2 in list.Values)
            {
                Console.WriteLine(item2);
            }
            Console.ReadKey();
        }
    }
}

在这里插入图片描述

c# List泛型集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _08List泛型集合
{
    class Program
    {
        static void Main(string[] args)
        {
            Program.Study01();
        }
        public static void Study01()
        {
            //创建int类型的泛型集合对象
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.AddRange(new int[] { 5, 6, 7, 8, 9 });
            list.AddRange(list);

            //list泛型集合可以转换成数组
            int[] sum = list.ToArray();

            foreach (int item in sum)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
            List<string> strList = new List<string>();
            strList.Add("小明");
            strList.Add("小红");
            strList.Add("小蓝");
            strList.AddRange(new string[] { "啊实打实", "是大大是", "dasdasd" });
            string[] str = strList.ToArray();
            //数组也可以转泛型集合
            List<string> arrString = str.ToList();
            foreach (string item2 in arrString)
            {
                Console.WriteLine(item2);
            }
            Console.ReadKey();

        }
    }
}

c# 字典集合

			//创建键值对 字典集合
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "张三");
            dic.Add(2, "张三1");
            dic.Add(3, "张三3");
            dic[1] = "覆盖的";
            foreach (KeyValuePair<int,string> item in dic)
            {
                Console.WriteLine("{0}---->{1}", item.Key, item.Value);
            }

            Console.ReadKey();

在这里插入图片描述

  • 集合的相关方法
    在这里插入图片描述

Path路径类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06path路径类
{
    class Program
    {
        static void Main(string[] args)
        {
            string pathStr = @"E:\2020study\visual Stidio\01\stydy02\allocation.txt";
            //获取文件名
            Console.WriteLine("获取文件名:{0}", Path.GetFileName(pathStr));
            //获取文件名不含扩展名
            Console.WriteLine("获取文件名不含扩展名:{0}", Path.GetFileNameWithoutExtension(pathStr));
            //获取扩展名
            Console.WriteLine("获取扩展名:{0}", Path.GetExtension(pathStr));
            //获取文件所在的文件夹的名称路径
            Console.WriteLine("获取文件所在的文件夹的名称路径:{0}", Path.GetDirectoryName(pathStr));
            //获取文件的全路径(绝对路径)
            Console.WriteLine("获取文件的全路径(绝对路径):{0}", Path.GetFullPath(pathStr));
            //连接两个字符作为路径
            Console.WriteLine("连接两个字符作为路径:{0}", Path.Combine(@"E:\a\", "b.txt"));
            Console.ReadKey();
        }
    }
}


C# File文件类

            File.Create(@"E:\2020study\vs2013\Study01\newStudy.txt");
            Console.WriteLine("创建成功");
            Console.ReadKey();
            //删除一个文件
            File.Delete(@"E:\2020study\vs2013\Study01\123.txt");
            Console.WriteLine("删除成功");
            Console.ReadKey();
            //复制一个文件
            File.Copy(@"E:\2020study\vs2013\Study01\OldCopy.txt", @"E:\2020study\vs2013\Study01\NewCopy.txt");
            Console.WriteLine("复制成功");
            Console.ReadKey();
            //剪切一个文件
            File.Move(@"E:\2020study\vs2013\Study01\NewCopy.txt", @"E:\2020study\vs2013\Study01\Move\NewCopy.txt");
            Console.WriteLine("剪切成功");
            Console.ReadKey();
  // 读 (读入一个字符串) 返回的是一个字节数组
           byte[] buffer = File.ReadAllBytes(@"E:\2020study\vs2013\Study01\新建文本文档.txt");
           //将字节数组中的每一个元素按照指定的编码格式解码成字符串
           //UTF-8 GB2312(简体中文) GBK(简体中文+繁体中文) ASCII  Unicode
           //(出现乱码就是a的编码格式以b的编码形式来打开文件就会出现乱码)
           String s = Encoding.GetEncoding("UTF-8").GetString(buffer);
           Console.WriteLine(s);
           Console.ReadKey();
  //读文件将每一行数据存在数组中
            string[] contens = File.ReadAllLines(@"E:\2020study\vs2013\Study01\新建文本文档.txt");
            foreach (string item in contens)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
  //获取所有行 返回一个字符串
            string text = File.ReadAllText(@"E:\2020study\vs2013\Study01\新建文本文档.txt");
            Console.WriteLine(text);
            Console.ReadKey();
 //写入文件 (覆盖写入 || 字节写入)
            string str = "今天天气好晴朗,处处好阳关1111";
            //需要将字符串转成字节数值
            byte[] buffer2 = Encoding.Default.GetBytes(str);
            File.WriteAllBytes(@"E:\2020study\vs2013\Study01\新建文本文档1.txt", buffer2);
            Console.WriteLine("写入成功");
            Console.ReadKey();
 //写入文件 通过数组的形式写入没一行
            File.WriteAllLines(@"E:\2020study\vs2013\Study01\新建文本文档222.txt", new string[] { "第一行内容", "第二行内容", "第三行内容" });
            Console.WriteLine("写入成功");
            Console.ReadKey();
 //写入文件 通过字符串的形式覆盖写入
            File.WriteAllText(@"E:\2020study\vs2013\Study01\新建文本文档222.txt", "覆盖写入");
            Console.WriteLine("写入成功");
            Console.ReadKey();   
  //追加
            File.AppendAllText(@"E:\2020study\vs2013\Study01\新建文本文档222.txt", "追加写入内容");
            Console.WriteLine("写入成功");
            Console.ReadKey();
  //追加
            File.AppendAllLines(@"E:\2020study\vs2013\Study01\新建文本文档222.txt", new string[] { "第一行内容", "第二行内容", "第三行内容" });
            Console.WriteLine("写入成功");
            Console.ReadKey();   

c# 文件流

  • 文件流读文件
		    FileStream fsRead = new FileStream(@"E:\2020study\vs2013\Study01\文件流练习2.txt", FileMode.OpenOrCreate, FileAccess.Read);
            byte[] buffer = new byte[1024 * 1024 * 5]; //5M 每次读5M
            //返回本次实际获取到的有效字节数
            int r = fsRead.Read(buffer, 0, buffer.Length); //从0开始取取5M的内容
            //将字节数组中的每一个元素按照指定的编码格式解码成字符串
            string s = Encoding.Default.GetString(buffer, 0, r);
            //关闭流
            fsRead.Close();
            //释放流所占用的资源
            fsRead.Dispose();
            Console.WriteLine(s);
            Console.Read();


  • 文件流写入文件
            //将文件流的过程写在using当中,会自动的帮助我们释放流所占用的内存
            using(FileStream fsWrile = new FileStream(@"E:\2020study\vs2013\Study01\文件流练习2.txt",FileMode.OpenOrCreate,FileAccess.Write)){
                string s = "插入的文字 巴拉巴拉巴拉巴拉123";
                //转字节
                byte[] buffer = Encoding.Default.GetBytes(s);
                fsWrile.Write(buffer,0,buffer.Length);
             }

            Console.WriteLine("插入成功");
            Console.Read();
  • 文件流复制多媒体文件
            string soucre = @"C:\Users\gj\Videos\Taylor\All Too Well.mp4"; //原路径
            string target = @"E:\2020study\vs2013\Study01\copyMP4.mp4";   //要复制到的路径
            //读文件的流
            using (FileStream fsRead = new FileStream(soucre, FileMode.OpenOrCreate,FileAccess.Read))
            {
                //写入文件的流
                using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    //定义每次读多大的内容 
                    byte[] buffer = new byte[1024 * 1024 * 5];
                    //因为一次读5M 文件过大时 需要循环去读
                    while (true)
                    {
                        //返回本次实际读取到的字节数 
                        int r = fsRead.Read(buffer, 0, buffer.Length);
                        //如果返回0 也就是读完了
                        if (r == 0)
                        {
                            break;
                        }
                        //写入
                        fsWrite.Write(buffer, 0, r);
                    }
                }
            }
            Console.WriteLine("复制成功");
            Console.ReadKey();

StreamReader和StreamWrite

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace _11_StreamReader和StreamWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用StreamReader来读一个文件
            using (StreamReader sr = new StreamReader(@"E:\2020study\vs2013\Study01\文件流练习2.txt",Encoding.Default))
            {
                while(!sr.EndOfStream){
                    Console.WriteLine(sr.ReadLine());
                }
            }
            Console.ReadKey();

            //SteamWrite写入文件
            using (StreamWriter strw = new StreamWriter(@"E:\2020study\vs2013\Study01\文件流练习.txt",true))
            {
                strw.WriteLine("插入的文字 巴拉巴拉巴拉巴拉123111");
            }
            Console.WriteLine("写入成功");
            Console.ReadKey();
        }
    }
}

Directory 文件夹类

 private void DirectoryStudy_Load(object sender, EventArgs e)
        {
            //如果没有这个文件就创建
            if (Directory.Exists("./Newfire/newTex.txt")) Directory.CreateDirectory("./Newfire/newTex.txt");


            //删除文件 (第二个参数为true时 文件下的文件一同删除,默认为false,当不是空文件时会报错)
            Directory.Delete("./123/1 - 副本 (3)",true);
             
            //剪切文件夹    原路径     , 新路径
            Directory.Move("./123/1 - 副本", "./123/1/new");

            //获取目录下的所有文件的全路径 并指定文件类型
            string[] imgs = Directory.GetFiles("./images", "*.jpg");

            //获取目录下的所有文件的全路径
            string[] files = Directory.GetDirectories("./123");
        }

在这里插入图片描述

----------c# 多态----------

实现多态的3个手段: 1、虚方法2、抽象类3、接口

多态之虚方法

  • 当父类的方法有实现(就是有方法体)有意义的时候 ,将父类的方法标记为虚方法(使用virtual关键字),这个函数可以被子类重新写一遍
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _12多态之虚方法
{
    class Program
    {
        //实现多态的3个手段 1、虚方法2、抽象类3、接口
        //1、虚方法:当父类的方法有实现(就是有方法体)有意义的时候 ,将父类的方法标记为虚方法(使用virtual关键字),这个函数可以被子类重新写一遍 
        static void Main(string[] args)
        {
            Teatch obj2 = new Teatch("刘");
            Persen obj1 = new Persen("小明");

            Persen[] onjs = { obj1, obj2 };
            for (int i = 0; i < onjs.Length; i++)
            {
                //如果不使用 virtual 这里调用的都是父类的Hell方法
                onjs[i].Hell();
            }

            Console.ReadKey();
        }

        public class Persen
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
           
            public  Persen(string name)
            {
                this.Name = name;
            }
            //1、虚方法:将父类的方法标记为虚方法(使用virtual关键字 )
            public virtual void Hell()
            {
                Console.WriteLine("你好,我是人类,我叫{0}", this.Name);
            }
        }

        public class Teatch : Persen
        {
           
            public Teatch(string name):base(name){

            }
            //虚方法:父类的方法标记 了virtual关键字 子类方法使用override关键字  重写一遍这个方法
            public override void Hell()
            {
                Console.WriteLine("你好,我是人类,我叫{0},我是一名老师", this.Name);
            }
        }
    }
}

在这里插入图片描述

多态之抽象类抽象方法

当父类的方法不知道怎么去实现的时候,可以考虑将父类写成抽象类(抽象类是不能new不能实现的),将方法写成抽象方法(抽象方法是不予许有方法体的!!!)
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _13多态之抽象类抽象方法
{
    class Program
    {
        //实现多态的3个手段 1、虚方法2、抽象类3、接口
        //2、抽象类:当父类的方法不知道怎么去实现的时候,可以考虑将父类写成抽象类(抽象类是不能new不能实现的),将方法写成抽象方法(抽象方法是不予许有方法体的!!!)
        static void Main(string[] args)
        {
            Animal dogObj = new Dog();
            dogObj.Hell();

            Console.ReadKey();
        }
        //定义抽象类
        public abstract class Animal
        {
            //定义抽象方法 (抽象方法是不予许有方法体的!!!)
            public abstract void Hell();
        }

        //创建子类
        public class Dog : Animal
        {
            public override void Hell()
            {
                Console.WriteLine("小狗汪汪叫");
            }
        }
        //创建子类
        public class Cat : Animal
        {
            public override void Hell()
            {
                Console.WriteLine("小猫喵喵叫");
            }
        }
    }
}

在这里插入图片描述

多态练习

通过多态模拟移动硬盘、u盘、mp3插到电脑上进行读写数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _15多态抽象练习02
{
    class Program
    {
        static void Main(string[] args)
        {
            //模拟移动硬盘、u盘、mp3插到电脑上进行读写数据
            Computer cpu = new Computer();
            MobileStorage u = new UDiks();
            MobileStorage m = new MobileDisk();
            //u盘插入
            cpu.Ms = u;
            cpu.CpuRead();
            //移动硬盘插入
            cpu.Ms = m;
            cpu.CpuRead();
            Console.ReadKey();
        }
        //父类
        public abstract class MobileStorage
        {
            public abstract void Read();
            public abstract void Write();
        }

        //移动硬盘类
        public class MobileDisk : MobileStorage
        {

            public override void Read()
            {
                Console.WriteLine("移动硬盘开始读数据");
            }

            public override void Write()
            {
                Console.WriteLine("移动硬盘开始写数据");
            }
        }
        //u盘类
        public class UDiks : MobileStorage
        {

            public override void Read()
            {
                Console.WriteLine("U盘开始读数据");
            }

            public override void Write()
            {
                Console.WriteLine("U盘开始写数据");
            }
        }

        //mp3类
        public class Mp3 : MobileStorage
        {

            public override void Read()
            {
                Console.WriteLine("MP3开始读数据");
            }

            public override void Write()
            {
                Console.WriteLine("MP3开始写数据");
            }
            //mp3有自己的方法
            public void Play()
            {
                Console.WriteLine("MP3开始播放音乐");

            }
        }


        //电脑类
        public class Computer
        {
            private MobileStorage _ms;

            internal MobileStorage Ms
            {
                get { return _ms; }
                set { _ms = value; }
            }
            public void CpuRead()
            {
                this.Ms.Read();
            }
            public void CpuWrite()
            {
                this.Ms.Write();
            }
        }

    }
}

在这里插入图片描述

值传递和引用传递

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16值传递和引用传递
{
    class Program
    {
        static void Main(string[] args)
        {
            //值类型:int double char decimal bool enum struct
            //引用类型:string 数组 自定义类 集合 object 接口

            int a = 1;
            int b = a;
            b = 2;
            Console.WriteLine(a); //1
            Console.WriteLine(b); //2
            Console.ReadKey();

            //值类型在传递的时候 传递的是这个值的本身
            //引用类型在传递的时候 是这个对象在堆中的地址
            Person p1 = new Person();
            p1.Name = "小明";
            Person p2 = p1;
            p2.Name = "小红";
            Console.WriteLine(p1.Name); //小红  由于p1是引用类型 所以传递的是地址 所以都是小红
            Console.WriteLine(p2.Name); //小红
            Console.ReadKey();


            string s1 = "小红";
            string s2 = s1;
            s2 = "小明";
            Console.WriteLine(s1); //小红 虽然string是引用类型 但string具有不可逆性 所以s1是小红 s2是小明
            Console.WriteLine(s2); //小明
            Console.ReadKey();
        }

        public class Person
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    }
}

序列化和方序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace _19序列化和反序列化
{
    class Program
    {
        static void Main(string[] args)
        {
            //将p对象序列化给电脑
            Person p = new Person();
            p.Name = "小明";
            p.Age = 18;
            using (FileStream fsWrite = new FileStream("./newExt.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                //开始序列化对象
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fsWrite, p);
            }
            Console.WriteLine("序列化成功");
            Console.ReadKey();




            //接收序列化进行返序列化
            Person p2;//
            using(FileStream fsRead = new FileStream("./newExt.txt",FileMode.OpenOrCreate,FileAccess.Read)){
                BinaryFormatter bf = new BinaryFormatter();
                p2 = (Person)bf.Deserialize(fsRead);
            }
            Console.WriteLine("放序列化成功");
            Console.WriteLine(p2.Name);
            Console.Read();
        }
        //将这个类标记成可序列化!!!
        [Serializable]
        public class Person
        {
            private string _name;
            private int _age;

            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            public Person()
            {

            }
        }
    }
}

部分类 partial

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20部分类
{
    class Program
    {
        static void Main(string[] args)
        {

        }
        // partial 声明为部分类 部分类之间的方法变量都是通用的
        public partial class Skyblue
        {

        }

        public partial class SkyBlue
        {

        }
    }

}

密封类 sealed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _21部分类
{
    class Program
    {
        static void Main(string[] args)
        {
           
        }
        //sealed 密封类 ;密封类可以继承其它类但其它类不能继承密封类 
        public sealed class Skyblue:Blue
        {

        }

        public class Blue
        {

        }
    }
}

重写父类的ToString方法override

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _23重写父类的ToString方法 
{
    class Program
    {
        static void Main(string[] args)
        {
            Skyblue s = new Skyblue();
            Console.WriteLine(s.ToString());
            Console.ReadKey();
        }

        public class Skyblue
        {
            public override string ToString()
            {
                return "我是重写后的ToString方法";
            }
        }
    }
}

接口的一些特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01接口的一些特性
{
    class Program
    {
        static void Main(string[] args)
        {
            //为了多态接口不能被实例化new
            SkyBlue s = new SkyBlue();
            s.Study01();
            Console.ReadKey();
            superClass s2 = new superClass();
            s2.i12();
            Console.ReadKey();

        }
        //接口定义 [public] interface I接口名able{}(接口名一般规范I开头able结尾)
        //接口中只能有方法、属性、索引器、事件、,不能有“字段”和构造函数
       
        //接口并不能继承类,而类可以继承接口

        //实现接口的子类必须实现接口的全部成员 
        public interface ISkyBlueable
        {
            //接口中的成员不容许添加任何访问修饰符 ,默认就是public
            void Study01();
            //不予许写就有方法体的函数
            string Study02();
           
        }


        public class SkyBlue:ISkyBlueable
        {

            public void Study01()
            {
                Console.WriteLine("接口方法01");
            }

            public string Study02()
            {
                Console.WriteLine("接口方法02");
                return "接口方法";
            }
        }
        //接口与接口之间可以继承和多继承
        public interface I01able
        {
            void i11();
        }
        public interface I02able
        {
            void i12();
        }
        public interface I03able
        {
            void i13();
        }
        public interface Isuperable:I01able,I02able,I03able
        {
            void superStudy();
        }
        //接口与接口之间可以继承和多继承
        public class superClass:Isuperable
        {
            //实现接口的子类必须实现接口的全部成员 
            public void superStudy()
            {
                Console.WriteLine("方法superStudy");
            }

            public void i11()
            {
                Console.WriteLine("方法i11");

            }

            public void i12()
            {
                Console.WriteLine("方法i12");

            }

            public void i13()
            {
                Console.WriteLine("方法i13");

            }
        }
        //如果一个类继承了父类又继承了接口那么父类必须写在前面
        public class Study01:SkyBlue,I03able
        {

            public void i13()
            {
                Console.WriteLine("aaaa");
            }
        }

    }
}

显示实现接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02显示实现接口
{
    class Program
    {
        static void Main(string[] args)
        {
            //当类自己的方法和接口要实现的方法名字一样时
            SkyBlue sk1 = new SkyBlue();
            sk1.study();
            ISkyBlueable isk = new SkyBlue();
            isk.study();

            Console.ReadKey();
        }

        public class SkyBlue:ISkyBlueable
        {
            
            public void study()
            {
                Console.WriteLine("我是类的方法");
            }
            //表明我是实现接口的study方法
            void ISkyBlueable.study()
            {
                Console.WriteLine("我是接口的方法");

            }
        }

        public interface ISkyBlueable
        {
            void study();
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值