C#入门笔记

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
F11逐步调试
断点调试
在这里插入图片描述
在这里插入图片描述

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

结构体

namespace demo01{

    public struct Person{
        public string _name;
        public int _age;
        public char _gender;
    }
    class Program{
        static void Main(string[] args){
            Person zhangsan;
            zhangsan._name = "张三";
            zhangsan._age = 21;
            zhangsan._gender = '男';
            Console.WriteLine("{0}", zhangsan);
            Console.ReadKey();
        }
    }
}

在这里插入图片描述
字符串转枚举类型
在这里插入图片描述

out 参数

namespace out参数
{
    class Program
    {
        static void Main(string[] args)
        {
            //写一个方法,求一个数组中的最大值、最小值、总和、平均值
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] res = getMaxMinSumAvg(numbers);
            Console.WriteLine("{0}\n{1}\n{2}\n{3}",res[0], res[1], res[2], res[3]);
            Console.ReadKey();
        }
        public static  int[] getMaxMinSumAvg(int[] nums)
        {
            int[] res = new int[4];
            // res[0] max
            // res[1] min ...
            res[0] = nums[0];
            res[1] = nums[0];
            res[2] = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > res[0])
                {
                    res[0] = nums[i];
                }
                if (nums[i] < res[1])
                {
                    res[1] = nums[i];
                }
                res[2] += nums[i];
            }
            res[3] = res[2]/ nums.Length;
            return res;
        }
    }
}

namespace out参数
{
    class Program
    {
        static void Main(string[] args)
        {
            //写一个方法,求一个数组中的最大值、最小值、总和、平均值
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int max, min, avg, sum;
            Test(numbers,out max,out min,out sum,out avg);
            Console.WriteLine("{0}\n{1}\n{2}\n{3}",max,min,sum,avg);
            Console.ReadKey();
        }
      
        public static void Test(int[] nums, out int max,out int min,out int sum,out int avg)
        {
            max = nums[0];
            min = nums[0];
            sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
                if (nums[i] < min)
                {
                    min = nums[i];
                }
               sum += nums[i];
            }
            avg = sum / nums.Length;
        }
    }
}

ref参数

namespace ref参数
{
    class Program
    {
        static void Main(string[] args)
        {
            double salary = 5000;
            jiangJin(ref salary);
            Console.WriteLine(salary);
            Console.ReadKey();
        }
        public static void jiangJin(ref double s)
        {
            s += 500;
        }
        public static void faKuan(ref double s)
        {
            s -= 500;
        }
    }
}
        static void Main(string[] args)
        {
            int a = 1, b = 2;
            swap(ref a, ref b);
            Console.WriteLine("{0}{1}",a, b);
            Console.ReadKey();
        }
        public static void swap(ref int  a,ref int b)
        {
            int t = a;
            a = b;
            b = t;
        }

param可变参数

        static void Main(string[] args)
        {
            //int[] s = { 100, 99, 98 };
            Test("张三", 99,98,100);
            Console.ReadKey();
        }
        public static void Test(string name,params int[] score)
        {
            int sum = 0;
            for(int i = 0; i < score.Length; i++)
            {
                sum += score[i];
            }
            Console.WriteLine(sum);
        }

namespace{
    public class Person
    {
        public string _name;
        public int _age;
        public char _gender;


        public void CHLSS()
        {
            Console.WriteLine("我叫{0},我今年{1}岁了,{2},我会吃喝拉撒睡",this._name,this._age,this._gender);
        }

    }
}


namespace{
    class Program
    {
        static void Main(string[] args)
        {
            Person zhangsan = new Person();
            zhangsan._name = "张三";
            zhangsan._age = 22;
            zhangsan._gender = '男';
            zhangsan.CHLSS();
            Console.ReadKey();
        }
    }
}

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

        public string name
        {
            get{ return name; }
            set{ name = value; }
        }

在这里插入图片描述

namespace{
    public class Person
    {
        private string _name;
        public string Name
        {
            get{ return _name; }
            set{ _name = value; }
        }
        private int _age;
        public int Age
        {
            get { return _age; }
            set {
                if(value < 0 || value > 100)
                {
                    value = 0;
                }
                _age = value;
            }
        }
        private char _gender;
        public char Gender
        {
            get { return _gender;}
            set {
                if (value != '男' && value != '女')
                {
                    value = '男';
                }
                _gender = value;
            }
        }


        public void CHLSS()
        {
            Console.WriteLine("我叫{0},我今年{1}岁了,{2},我会吃喝拉撒睡",this.Name,this.Age,this.Gender);
        }

    }
}

namespace{
    class Program
    {
        static void Main(string[] args)
        {
            Person zhangsan = new Person();
            zhangsan.Name = "张三三";
            zhangsan.Age = 22;
            zhangsan.Gender = '男';
            zhangsan.CHLSS();
            Console.ReadKey();
        }
    }
}

在这里插入图片描述
静态类中只能有静态方法,不能声明实例成员
静态类不能创建对象

构造函数

        public Person(string name,int age,char gender)
        {
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
        }

在这里插入图片描述
在这里插入图片描述
构造函数嵌套

        public Person(string name,int age,char gender)
        {
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
        }
        public Person(string name):this(name,99,'男')
        {
            
        }

析构函数

        //当程序结束的时候 析构函数才执行
        // 帮助我们释放资源
        // GC garbage collection
        ~Person()
        {
            Console.WriteLine("我是析构函数");
        }

namespace

引用其他项目的类
在这里插入图片描述
在这里插入图片描述

string

            string str = "abcdef";
            char[] chars = str.ToCharArray();
            chars[0] = 'k';
            str = new string(chars);

在这里插入图片描述

            char[] chs = { ' ', ',' };
            string[] strs = str.Split(chs);

        static void Main(string[] args)
        {
            string s = "2008-08-08  ";
            char[] chs = { '-' };
            //移除空白
            string[] strs = s.Split(chs, StringSplitOptions.RemoveEmptyEntries);
            for(int i = 0; i < strs.Length; i++)
            {
                Console.WriteLine(strs[i]);
            }
            Console.ReadKey();
        }

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

ArrayList

  static void Main(string[] args)
        {


            ArrayList list = new ArrayList();
            list.Add(1);
            list.Add(3.14);
            list.Add("hhh");
            list.Add(200m);
            list.Add(new int[] { 1,2,3,4});
            list.Add(list);
            for(int i = 0; i  < list.Count; i++)
            {
                if(list[i] is int[])
                {
                    for(int j = 0; j < ((int[])list[i]).Length; j++)
                    {
                        Console.WriteLine(((int[])list[i])[j]);
                    }
                }
                else
                {
                    Console.WriteLine(list[i]);
                }
               
            }

            Console.ReadKey();
        }
    }
            //添加集合元素
            list.AddRange(new int[] {1,2,3,4});

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

HashTable

        static void Main(string[] args)
        {

            Hashtable ht = new Hashtable();
            ht.Add(1, "zs");
            ht.Add(2, "ls");
            ht.Add(3, "wem");
            ht[4] = 4;
            // h[1] 就是 "zs"
          
            Console.WriteLine(ht[1]);
            Console.WriteLine(ht[2]);
            Console.WriteLine(ht[3]);

            Console.ReadKey();
        }
            //遍历键
            foreach(var item in ht.Keys)
            {
                Console.WriteLine(ht[item]);
            }

在这里插入图片描述

Path类

using System.IO;

 static void Main(string[] args)
        {
            string str = @"C:\Users\Thinkpad\Desktop\01.txt";

            string res = Path.GetFileName(str);
            string res2 = Path.GetFileNameWithoutExtension(str);
            string res3 = Path.GetExtension(str);
            string res4 = Path.GetDirectoryName(str);
            string res5 = Path.GetFullPath(str);
            string res6 = Path.Combine(@"C:\Users\", "a.avi");
            Console.WriteLine(res); //01.txt
            Console.WriteLine(res2); //01
            Console.WriteLine(res3); // .txt
            Console.WriteLine(res4); //C:\Users\Thinkpad\Desktop
            Console.WriteLine(res5);
            Console.WriteLine(res6);
            Console.ReadKey();
        }

File类

        static void Main(string[] args)
        {
            //创建一个文件
            File.Create(@"C:\Users\Thinkpad\Desktop\02.txt");
            //删除一个文件
            File.Delete(@"C:\Users\Thinkpad\Desktop\02.txt");
            //复制一个文件
            File.Copy(@"C:\Users\Thinkpad\Desktop\02.txt", @"C:\Users\Thinkpad\Desktop\03.txt");
        }
  • 读文本
        static void Main(string[] args)
        {
           string[] strs =  File.ReadAllLines(@"C:\Users\Thinkpad\Desktop\02.txt");
            foreach(var item in strs)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
           string strs =  File.ReadAllText(@"C:\Users\Thinkpad\Desktop\02.txt");
      
           Console.WriteLine(strs);
           Console.ReadKey();

相对路径

        static void Main(string[] args)
        {
           string strs =  File.ReadAllText(@"02.txt");
      
           Console.WriteLine(strs);
           Console.ReadKey();
        }

        static void Main(string[] args)
        {
           File.WriteAllLines(@"02.txt",new string[] { "213","11","45"});
     
           Console.ReadKey();
        }

覆盖写入

           File.WriteAllText(@"02.txt","superkcl");
     
           Console.ReadKey();

追加写入

            File.AppendAllText(@"02.txt", "superkcl");
           Console.ReadKey();

List泛型

 static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            foreach(var item in list)
            {
                Console.WriteLine(item);
            }
            //list转为数组
            int[] nums = list.ToArray();
            //数组转为list
            List<int> list2 = nums.ToList();
            //char数组转list
            char[] chs = new char[] { 'a', 'n' };
            List<char> list3 = chs.ToList();
            Console.ReadKey();
        }

Dictionary

  static void Main(string[] args)
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "zs");
            dic.Add(2, "ls");
            dic.Add(3, "wrm");
            dic[1] = "hhh";
            foreach(var item in dic.Keys)
            {
                Console.WriteLine(dic[item]);
            }
            foreach(KeyValuePair<int,string> item in dic)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }
            Console.ReadKey();
        }

多态之虚函数


namespace 多态
{
    public class Person
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public Person(string name)
        {
            this.Name = name;
        }
        //虚方法,这个函数被子类重写
        public virtual void SayHello()
        {
            Console.WriteLine("我是person");
        }
    }
    public class Chinese : Person
    {
        public Chinese(string name) : base(name)
        {

        }
        //重写父类虚方法
        public override void SayHello()
        {
            Console.WriteLine("我是chinese,我叫{0}",this.Name);
        }
    }
    public class Japanese: Person
    {
        public Japanese(string name) : base(name)
        {

        }
        public override void SayHello()
        {
            Console.WriteLine("我是Japanese,我叫{0}", this.Name);
        }
    }
    public class American : Person
    {
        public American(string name) : base(name)
        {

        }
        public override  void SayHello()
        {
            Console.WriteLine("American,我叫{0}", this.Name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            
            Chinese cn1 = new Chinese("韩梅梅");
            Chinese cn2 = new Chinese("李雷");
            Japanese j1 = new Japanese("树下君");
            Japanese j2 = new Japanese("井边子");
            American a1 = new American("tom");
            American a2 = new American("jack");
            Person[] persons =  { cn1, cn2, j1, j2, a1, a2 };
            for(int i = 0; i < persons.Length; i++)
            {
                persons[i].SayHello();
                if(persons[i] is Chinese)
                {
                    ((Chinese)persons[i]).SayHello();
                }
            }
            Console.ReadKey();
        }
    }
}

抽象类

FileStream

            // FileStream操作字节的
            string path = @"C:\Users\Thinkpad\Desktop\02.txt";
            //常用的有openOrCreate append
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] buffer = new byte[1024 * 1024 * 2]; // 2M
            //本次实际读取到的有效字节数
            int r = fs.Read(buffer,0,buffer.Length);
            //将字节数数组中每个元素解码
            string s = Encoding.UTF8.GetString(buffer, 0, r);
            //关闭流
            fs.Close();
            //释放流所占用的资源
            fs.Dispose();
         using (filestream fs = new filestream(@"c:\users\thinkpad\desktop\02.txt", filemode.openorcreate, fileaccess.write))
            {
                string str = "哈哈哈哈";
                byte[] buffer = encoding.utf8.getbytes(str);
                fs.write(buffer, 0, buffer.length);
            }
            console.writeline("写入ok");
            console.readkey();

Directory

private void button1_Click(object sender, EventArgs e)
        {
             Directory.CreateDirectory(@"C:\Users\Thinkpad\Desktop\a");
             MessageBox.Show("创建成功");
            Directory.Delete(@"C:\Users\Thinkpad\Desktop\a",true);

            Directory.Move(@"C:\Users\Thinkpad\Desktop\a", @"C:\Users\Thinkpad\Desktop\b");

            //获得该文件夹下面所有文件的全路径
            string[] path = Directory.GetFiles(@"C:\Users\Thinkpad\Pictures\壁纸","*.jpg");
            for(int i = 0; i < path.Length; i++)
            {
                MessageBox.Show(path[i]);
            }

打开文件对话框

 private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择要打开的文件";
            ofd.Multiselect = true; //多选
            ofd.InitialDirectory = @"C:\Users\Thinkpad\Desktop";
            ofd.Filter = "文本文件|*.txt|媒体文件|*.wmv";
            ofd.ShowDialog();

            //获得打开对话框中文件的路径
            string path = ofd.FileName;
            if (path == "") return;
            using (FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read))
            {
                byte[] buffer = new byte[1024 * 1024 * 2];
               int l =  fs.Read(buffer, 0, buffer.Length);
                textBox1.Text = Encoding.UTF8.GetString(buffer, 0, l);

            }
        }

保存文件对话框

private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "请选择要保存的路径";
            sfd.InitialDirectory = @"C:\Users\Thinkpad\Desktop";
            sfd.Filter = "文本文件|*.txt";
            sfd.ShowDialog();

            string path = sfd.FileName;
            if (path == "") return;
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
                fs.Write(buffer, 0, buffer.Length);
            }
            MessageBox.Show("保存成功");
        }

字体颜色对话框

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值