zoukankan      html  css  js  c++  java
  • C#基本总结

    一、为字段赋值的两种方式:1. 通过属性的形式,2. 通过构造函数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person p = new Person('');
                //p.Name = "李四";
                //p.Age = -19;
                //Console.WriteLine(p.Age);
                Console.WriteLine(p.Gender);
                Console.ReadKey();
            }
        }
        class Person
        {
            //字段、属性、函数、构造函数....
            //字段:存储数据
            //属性:保护字段 get set
            //函数:描述对象的行为
            //构造函数:初始化对象,给对象的每个属性进行赋值
    
            string _name;
    
            public string Name//张三
            {
                get { return _name; }//去属性的值
                set
                {
                    if (value != "张三")
                    {
                        value = "张三";
                    }
                    _name = value;
                }//给属性赋值
            }
    
            int _age;
    
            public int Age
            {
                get
                {
                    if (_age < 0 || _age > 100)
                    {
                        return _age = 18;
                    }
                    return _age;
                }
                set { _age = value; }
            }
    
            public char Gender { get; set; }
    
    
            /// <summary>
            /// 通过构造函数的形式给字段赋值
            /// </summary>
            /// <param name="gender"></param>
            public Person(char gender)
            {
                if (gender != '' && gender != '')
                {
                    gender = '';
                }
                this.Gender = gender;
            }
        }
    
    }
    为字段赋值

    二、3个关键字的总结:new,this,base。

    1. new:创建对象,隐藏父类成员

    创建对象:Student s = new Student();

    隐藏父类成员:如果使用非重写的方法,需要用关键字new来进行覆盖,具体代码如下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Student s = new Student();
            }
        }
        class Person
        {
            public void SayHello()
            {
                Console.WriteLine("我是人类");
            }
        }
        class Student : Person
        {
            /// <summary>
            /// 如果使用非重写的方法,需要用关键字new来进行覆盖
            /// </summary>
            public new void SayHello()//彻底隐藏了父类的SayHello()
            {
                Console.WriteLine("我是学生");
            }
        }
    }
    隐藏父类成员

    2. this:代表当前类的对象。

       class Teacher
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public char Gender { get; set; }
    
            public int Chinese { get; set; }
            public int Math { get; set; }
            public int English { get; set; }
    
    
            public Teacher(string name, int age, char gender, int chinese, int math, int english)
            {
                this.Name = name;
                this.Age = age;
                this.Gender = gender;
                this.Chinese = chinese;
                this.Math = math;
                this.English = english;
            }
    
    
            public Teacher(string name, int age, char gender)
                : this(name, age, gender
                    , 0, 0, 0)
            {
    
            }
    
    
            public Teacher(string name, int chinese, int math, int english)
                : this(name, 0, '', chinese, math, english)
            { }
    
    
            public void SayHi()
            {
                Console.WriteLine("我叫{0},今年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
            }
    
            public void ShowScore()
            {
                Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English) / 3);
            }
        }
    this关键字

    3. base:调用父类的成员

        class Person
        {
            public void SayHello()
            {
                Console.WriteLine("我是人类");
            }
        }
    
        class Student : Person
        {
            /// <summary>
            /// 
            /// </summary>
            public new void SayHello()//彻底隐藏了父类的SayHello()
            {
                Console.WriteLine("我是学生");
            }
    
            public Student GetStudent()
            {
                return this;
            }
            /// <summary>
            /// 调用父类的成员
            /// </summary>
            public void GetPerson()
            {
                base.SayHello();
            }
        }
    base:调用父类成员

     三、泛型集合的两种形式:list泛型集合、Dictionary键值对集合

    常用操作:

    7、List<T>常用的函数
    Add():添加单个元素
    AddRange():添加一个集合
    Insert():插入一个元素
    InsertRange():插入一个集合
    Remove():移除指定的元素
    RemoveAt():根据下标移除元素
    RemoveRange():移除一定范围内的元素
    ToArray():集合转换成数组
    ToList():数组转换成集合

    1.list泛型的用法和示例

    using System;
    using System.Collections.Generic;  //必须引用
    using System.Linq;
    using System.Text;
    
    namespace _07List泛型集合
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<int> list = new List<int>();
                //集合--->数组
                //Count:获取集合中实际包含的元素的个数
                //Capcity:集合中可以包含的元素的个数
    
                //list.Add(1);
                //Console.WriteLine(list.Count);
                //Console.WriteLine(list.Capacity);
    
                //Add的是添加单个元素
                //AddRange是添加集合
                list.Add(100);
                list.AddRange(new int[] { 1, 2, 3, 4, 5, 6 });
    
                //list.Remove(100);
                //list.RemoveAll(n => n > 3);
    
                //list.RemoveAt(3);
    
                //list.RemoveRange(1, 6);
    
                // list.Insert(1, 200);
    
                // list.InsertRange(0, new int[] { 5, 4, 3, 2, 1 });
    
                //集合跟数组之间的转换
    
                //集合----->数组
                int[] nums = list.ToArray();
    
                List<string> list2 = new List<string>();
    
                //list2.ToArray()
    
                int[] nums3 = { 1, 2, 3, 4, 5, 6 };
    
                List<int> list3 = nums3.ToList();
    
               
                for (int i = 0; i < list3.Count; i++)
                {
                    Console.WriteLine(list3[i]);
                }
    
                Console.ReadKey();
            }
        }
    }
    list泛型集合

    list泛型的练习:(1).去除两个集合中的重复值 (2).随机生成10个1-100之间的数放到List中,要求这10个数不能重复,并且都是偶数 (3).把分拣奇偶数的程序用泛型实现

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _08List泛型集合的三个练习
    {
        class Program
        {
            static void Main(string[] args)
            {
                //案例:两个(List)集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个。
    
                //集合初始化器
                //List<string> list1 = new List<string>() { "a", "b", "c", "d", "e" };
                //List<string> list2 = new List<string>() { "d", "e", "f", "g", "h" };
    
                ////让list1添加list2中的每个元素
                //for (int i = 0; i < list2.Count; i++)
                //{
                //    if (!list1.Contains(list2[i]))
                //    {
                //        list1.Add(list2[i]);
                //    }
                //}
    
                //foreach (string item in list1)
                //{
                //    Console.WriteLine(item);
                //}
    
    
    
                //案例:随机生成10个1-100之间的数放到List中,要求这10个数不能重复,并且都是偶数(添加10次,可能循环很多次。)
                //Random r = new Random();
                //List<int> list = new List<int>();
                //for (int i = 0; i <10; i++)
                //{
                //    int rNumber = r.Next(1, 101);
                //    if (!list.Contains(rNumber) && rNumber % 2 == 0)
                //    {
                //        list.Add(rNumber);
                //    }
                //    else
                //    {
                //        i--;
                //    }
                //}
    
                //foreach (int item in list)
                //{
                //    Console.WriteLine(item);
                //}
    
    
                //把分拣奇偶数的程序用泛型实现。List<int>
                int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                List<int> listJi = new List<int>();
                List<int> listOu = new List<int>();
    
                for (int i = 0; i < nums.Length; i++)
                {
                    if (nums[i] % 2 == 0)
                    {
                        listOu.Add(nums[i]);
                    }
                    else
                    {
                        listJi.Add(nums[i]);
                    }
                }
    
                //listJi.AddRange(listOu);
    
                //foreach (int item in listJi)
                //{
                //    Console.WriteLine(item);
                //}
                listOu.AddRange(listJi);
                foreach (int item in listOu)
                {
                    Console.WriteLine(item);
                }
    
    
                Console.ReadKey();
    
            }
        }
    }
    三个练习

    2. Dictionary键值对集合

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _09Dictionary键值对集合
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Dictionary<string, string> dic = new Dictionary<string, string>();
                //dic.Add("1", "张三");
                //dic.Add("2", "李四");
                //dic.Add("3", "王五");
                //dic.Add("4", "赵六");
                ////键值对集合中的键必须是唯一的
                ////dic.Add(4, "田七");
                ////键值对集合中的值是可以重复的
                //dic.Add("5", "王五");
                ////可以给键值对集合中的某一个值进行重新赋值
                //dic["4"] = "田七";
                //键值对集合常用函数
                //判断集合中是否已经包含某一个键
                //if (!dic.ContainsKey(13))
                //{
                //    dic.Add(13, "玩狗蛋儿");
                //}
                //else
                //{
                //    dic[3] = "王狗蛋儿";
                //}
                //Console.WriteLine(dic[13]);
    
                //使用foreach循环,通过遍历键值对的形式对键值对集合进行遍历
                //第一种遍历方式:遍历集合中的键
                //foreach (string item in dic.Keys)
                //{
                //    Console.WriteLine("键---{0}            值---{1}",item,dic[item]);
                //}
    
                //第二种遍历方式:遍历集合中的键值对
                //foreach (KeyValuePair<string, string> kv in dic)
                //{
                //    Console.WriteLine("键---{0}            值---{1}", kv.Key, kv.Value);
                //}
    
                //Console.WriteLine(dic[3]);//整体代表的就是值
               // Console.ReadKey();
    
            }
        }
    }
    Dictionary键值对集合

     四、文件的操作

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace _14File类的操作
    {
        class Program
        {
            static void Main(string[] args)
            {
                //File
                //Exist():判断指定的文件是否存在
                //Create():创建
                //Move():剪切
                //Copy():复制
                //Delete():删除
    
    
                //if (!File.Exists("1.txt"))
                //{
                //    File.Create("1.txt");
                //}
    
                //if (File.Exists("1.txt"))
                //{
                //    File.Delete("1.txt");
                //}
                //
    
                //File.Copy(@"C:UsersSpringRainDesktopenglish.txt", @"D:aaaaaa.txt");
                //Console.WriteLine("操作成功");
                //File.Move(@"D:aaaaaa.txt", @"C:UsersSpringRainDesktopbbbbb.txt");
    
                //ReadAllLines()默认采用的编码格式是utf-8
                //string[] str = File.ReadAllLines(@"C:UsersSpringRainDesktopenglish.txt");
                //ReadAllText()默认采用的编码格式也是utf-8
                //string str = File.ReadAllText(@"C:UsersSpringRainDesktopenglish.txt");
                //Console.WriteLine(str);
    
                //byte[] buffer = File.ReadAllBytes(@"C:UsersSpringRainDesktopenglish.txt");
                ////字节数组---->字符串
                //string str = Encoding.UTF8.GetString(buffer);
                //Console.WriteLine(str);
                //string str="张三李四王五百家姓";
                ////字符串--->字节数组
                //byte[] buffer= Encoding.Default.GetBytes(str);
                //File.WriteAllBytes(@"C:UsersSpringRainDesktop121212.txt", buffer);
                //Console.WriteLine("写入成功");
                //Console.ReadKey();
                //File.WriteAllLines(@"C:UsersSpringRainDesktop121212.txt", new string[] { "a", "b" });
    
              //  File.WriteAllText(@"C:UsersSpringRainDesktop121212.txt", "c");
    
                //byte[] buffer = new byte[3];
                //Console.WriteLine(buffer.ToString());
                //18 99 221 我爱你
               // Encoding.Default.GetString()
    
            }
        }
    }
    文件的操作

    五、MD5加密

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace _15MD5加密
    {
        class Program
        {
            static void Main(string[] args)
            {
                //张三abc  abc123  明文保存
    
                string str = "123";
                //202cb962ac59075b964b07152d234b70
                //202cb962ac59075b964b07152d234b70
                //202cb962ac5975b964b7152d234b70
                //3244185981728979115075721453575112
                string md5Str = GetMd5(str);
                Console.WriteLine(md5Str);
                Console.ReadKey();
            }
    
            static string GetMd5(string str)
            {
                MD5 md5 = MD5.Create();
                byte[] buffer = Encoding.Default.GetBytes(str);
                //开始加密 返回加密好的字节数组
                byte[] bufferMd5 = md5.ComputeHash(buffer);
                //转成字符串
                //string result = Encoding.Default.GetString(bufferMd5);
                //return result;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < bufferMd5.Length; i++)
                {
                    sb.Append(bufferMd5[i].ToString("x2"));//x:表示将十进制转换为16进制
                }
                return sb.ToString();
            }
        }
    }
    MD5加密
  • 相关阅读:
    BZOJ1511: [POI2006]OKR-Periods of Words
    BZOJ1009: [HNOI2008]GT考试
    BZOJ1355: [Baltic2009]Radio Transmission
    BZOJ1415: [Noi2005]聪聪和可可
    BZOJ1004: [HNOI2008]Cards
    UVA11077 Find the Permutations
    LA3641 Leonardo's Notebook
    UVA10294 Arif in Dhaka
    UVA11762 Race to 1
    UVA11427 Expect the Expected
  • 原文地址:https://www.cnblogs.com/wangjinya/p/11616426.html
Copyright © 2011-2022 走看看