C#基础二刷

2022-08-23  本文已影响0人  山猪打不过家猪
1. 字符串格式化
static void Main(string[] args)
{
    //方法一:
    string name = "Fxx";
    string email_type = "qq";
    string my_info = "邮箱用户名:{0};邮箱类型{1}";
    my_info = string.Format(my_info, name, email_type);
    //方法二:
    string a = string.Format("邮箱用户名:{0};邮箱类型{1}", name, email_type);
    //方法三:
    string b = $"邮箱用户名:{name};邮箱类型{email_type}";
}

2. 数组
static void Main(string[] args)
{
    //方法一
    int[] aa = new int[] { 1, 2, 3, 4, 5 };
    //方法二:
    int[] bb = { 1, 2, 3, 4, 5 };
}

static void Main(string[] args)
{
    int[] aa = new int[] { 1, 2, 3, 4, 5 };
    //foreach(元素类型 变量名 in 集合或者数组名)
    foreach (int i in aa)
    {
        Console.WriteLine(i);
    }
}
3. 面向对象该思考的问题
image.png
4. 类的定义规范
image.png

namespace ConsoleApp1
{

    /// <summary>
    /// 学员类
    /// </summary>
    class Student
    {
        //字段:ID(私有)
        private int stuId;
        //字段:姓名
        private string stuName;
        //属性:ID(公有)
        public int StudId
        {
            get { return stuId; }
            set { stuId = value; }
        }
        //属性:姓名
        public string StuName
        {
            get { return stuName; }
            set { stuName = value; }
        }

        //方法:获取学院信息
        public void GetStudent()
        {
            string stuInfo = string.Format("姓名:{0},学号{1}.", StudId, StuName);
            Console.WriteLine(stuInfo);
        }
    }
}

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建对象
            Student stu1 = new Student();
            //给属性赋值
            stu1.StudId = 1;
            stu1.StuName = "fxx";
            //调用对象方法
            stu1.GetStudent();
        }
    }   
}
5. 类的方法

访问修饰符(private,public) 返回类型(void,string,int,对象,任何类型都可以当返回值) 方法名(数据类型 参数名1,数据类型 参数名2){
//方法
}

6. 构造方法
//加上这个就可用对象.属性来添加
public Student()
{

}
public Student(int id)
{
    this.StudId = id;
}
public Student(int id, string name, int age):this(id)
        {
    this.StuName = name;
    this.Age = age;
}

7. 对象初始化器

与构造方法的差别:
相同点:都可以完成对象的初始化
不同点:
①构造方法具有强制化,必须一一对应(希望用户初始化),初始化器没有强制性
②初始化器只能对属性进行初始化,而构造方法可以对任何需要初始化的工作进行,如对象创建是读取文件等
③初始化器在创建对象时候使用,而构造方法卸载类里面


image.png
Stedent stu = new  Student(){
    Birthday = Convert.ToDateTime("19910101"),
    StudentId = 10005,
    StudentName = "Lil Red"
}
8. 泛型集合List<T>

使用泛型集合只能添加一种类型的数据,数据取出后无需强制转换,拆箱
T是Type的简写, 表示可以是任何不确定的具体类型。

namespace ConsoleApp1
{
    class Student
    {
        public Student() { }
        public Student(int id,string stuNam)
        {
            this.Id = id;
            this.StuName = stuNam;
        }
        public int Id { get; set; }
        public string StuName { get; set; }
    }
}

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建多个学员对象
            Student stu1 = new Student(1001,"fxx1");
            Student stu2= new Student(1002,"fxx2");
            Student stu3 = new Student(1003,"fxx3");
            Student stu4 = new Student(1004,"fxx4");
            Student stu5 = new Student(){};
            //创建集合添加元素
            List<Student> stuList = new List<Student>();
            stuList.Add(stu1);
            stuList.Add(stu2);
            stuList.Add(stu3);
            stuList.Add(stu4);
            stuList.Add(new Student() { 
                StuName ="fxx5",
                Id =1005      
            });
            //获取元素个数
            Console.WriteLine("集合内的元素个数是:{0}",stuList.Count);
            //删除元素
            stuList.RemoveAt(0);
            stuList.Remove(stu5);
            //遍历泛型集合
            foreach (Student stu in stuList)
            {
                Console.WriteLine($"学员姓名:{stu.StuName},学员ID{stu.Id}");

            }
        }
    }
}

8. 泛型字典Dictionary<K,V>
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建多个学员对象
            Student stu1 = new Student(1001,"fxx1");
            Student stu2= new Student(1002,"fxx2");
            Student stu3 = new Student(1003,"fxx3");
            Student stu4 = new Student(1004,"fxx4");
            //创建字典
            Dictionary<string, Student> stuDic = new Dictionary<string, Student>();
            //添加成员
            stuDic.Add("Fxx1", stu1);
            stuDic.Add("Fxx2", stu2);
            stuDic.Add("Fxx3", stu3);
            stuDic.Add("Fxx4", stu4);
            //遍历字典
            foreach (KeyValuePair<string, Student> item in stuDic)
            {
                Console.WriteLine($"字典的Key是{item.Key},字段的值是{item.Value},值里的学生信息是{item.Value.StuName}");
            }
        }
    }
}

9.集合排序

默认排序
普通类型的集合直接使用List.sort()

//普通集合
List<string> nameList = new List<string>() { "fxx2", "fxx1", "fxx3", "fxx5" };
//升序
nameList.Sort();

对象类型需要让排序的对象继承排序接口

class Student : IComparable<Student>
{
    public Student() { }
    public Student(int id, string stuNam)
    {
        this.Id = id;
        this.StuName = stuNam;
    }
    public int Id { get; set; }
    public string StuName { get; set; }

    //接口的实现接口
    public int CompareTo(Student other)
    {
        //降序(other在前降序)
        //return other.Id.CompareTo(this.Id);
        //other在后,升序
        return this.Id.CompareTo(other.Id);
    }
}
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建多个学员对象
            Student stu1 = new Student(1001,"fxx1");
            Student stu2= new Student(1002,"fxx2");
            Student stu3 = new Student(1003,"fxx3");
            Student stu4 = new Student(1004,"fxx4");

            //对象集合
            List<Student> stuList = new List<Student>() { stu2, stu4, stu3, stu1 };
            stuList.Sort();
            foreach (Student item in stuList)
            {
                Console.WriteLine(item.Id+item.StuName);
            }
        }
    }
}

对象动态排序


namespace ConsoleApp1
{
    class Student
    {
        public Student() { }
        public Student(int id,string stuNam)
        {
            this.Id = id;
            this.StuName = stuNam;
        }
        public int Id { get; set; }
        public string StuName { get; set; }

    }
    #region 四个排序类
    //添加四个排序类
    class StuNameAsc : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            //升序
            return x.StuName.CompareTo(y.StuName);
        }
    }
    class StuNameDesc : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return y.StuName.CompareTo(x.StuName);
        }

    }
    class IdAsc : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Id.CompareTo(y.Id);
        }

    }
    class IdDesc
    {
        public int Compare(Student x, Student y)
        {
            return y.Id.CompareTo(x.Id);
        }

    }

    #endregion
}

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建多个学员对象
            Student stu1 = new Student(1001,"fxx1");
            Student stu2= new Student(1002,"fxx2");
            Student stu3 = new Student(1003,"fxx3");
            Student stu4 = new Student(1004,"fxx4");

            //对象集合
            List<Student> stuList = new List<Student>() { stu2, stu4, stu3, stu1 };
            //姓名升序
            stuList.Sort(new StuNameAsc());
            foreach (Student item in stuList)
            {
                Console.WriteLine(item.Id+item.StuName);
            }
            //姓名降序
            stuList.Sort(new StuNameDesc());
            foreach (Student item in stuList)
            {
                Console.WriteLine(item.Id + item.StuName);
            }
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读