c#-中级篇知识合集-part01

这篇博客详细介绍了C#的中级知识,包括调试技巧、错误处理,特别是异常处理机制,深入讲解了面向对象编程中的类、匿名类型、值类型与引用类型的区别、继承、密封类和密封方法以及接口的定义和实现。同时,通过多个代码示例展示了如何在实际编程中应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

001-调试和错误处理

设置断点的方法:F9

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

namespace _001_调试和错误处理
{
    class Program
    {
        static void Main(string[] args)
        {
            int temp;
            int num1;
            int num2;
     //       int sum = num1 * num2;//未初始化变量不能使用。
            num1 = 1;
            num2 = 1;
       
        }
    }
}

002-中断模式下的调试

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

namespace _002_中断模式下的调试
{
    class Program
    {
        static void test()
        {
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            int num1 = 1;
            int num2 = 1;//F9添加/取消断点,或者在行首点击
            int num3 = num1 + num2;
            
           test();//逐语句,会跳到函数内部,逐过程,不会
            string name = "pengrui";
            Console.WriteLine(num3);
            Console.WriteLine(name);

            Console.ReadKey();
		}
    }
}

003-错误处理-异常处理

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

namespace _003_错误处理_异常处理
{
    //try...catch...finally//catch...finally至少得有一个
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int[] myArray = { 1, 2, 3, 4 };
                int myEle = myArray[4];
            }
          //  catch(IndexOutOfRangeException)
            catch(NullReferenceException)//在这里虽然写了异常捕获的程序,但是我们捕捉的类型不对,程序还是会中断
            {
                Console.WriteLine("发生了IndexOutOfRangeException");
                Console.WriteLine("您访问数组时,下标越界了");
            }
            finally
            {
                Console.WriteLine("TEST");
                 
            }
            Console.ReadKey(); 
        }
    }
}

004-异常处理-案例2

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

namespace _004_异常处理_案例2
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2;

            Console.WriteLine("请输入第一个数字");
            while (true)
            {
                try
                {
                    num1 = Convert.ToInt32(Console.ReadLine());//在try块中,如果有一行代码发生了异常,那么try块中剩余的代码就不会执行
                    break;
                }
                catch
                {
                    Console.WriteLine("您输入的不是一个整数,请重新输入");
                }
            }
            Console.WriteLine("请输入第二个数字");
            while (true)
            {
                try
                {
                    num2 = Convert.ToInt32(Console.ReadLine());//在try块中,如果有一行代码发生了异常,那么try块中剩余的代码就不会执行
                    break;
                }
                catch
                {
                    Console.WriteLine("您输入的不是一个整数,请重新输入");
                }
            }
            int num = num1 + num2;
            Console.WriteLine(num);
            Console.ReadKey();
        }   
    }
}

005-面向对象编程-类

custmer.cs

//Custmer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _005_面向对象编程_类
{
    //在这里我们定义了一个新的类叫做Customer
    class Customer
    {
        //数据成员:里面包含了4个字段
        public string name;
        public string address;
        public int age;
        public string buyTime;
        //函数成员:定义了一个方法
        public void Show()
        {
            Console.WriteLine("名字:"+name);
            Console.WriteLine("年龄:"+age);
            Console.WriteLine("地址:"+address);
            Console.WriteLine("购买时间:"+buyTime);
        }
    }
}

Program.cs

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

namespace _005_面向对象编程_类
{
    class Program
    {
        static void Main(string[] args)
        {
           //1,如果要使用一个类的话要引入它所在的命名空间,因为Customer位于当前的命名空间下,所以不需要引用,我们可以直接使用Customer.
            Customer customer1;
            customer1 = new Customer();//对对象初始化,需要new+类名().
            customer1.name = "siki";//我们自己定义的类声明的对象,需要先进行初始化,才能使用。
            Console.WriteLine(customer1.name);
            customer1.Show();//使用对象的方法
            Console.ReadKey(); 
        }
    }
}

006-类的定义和声明

Program.cs

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

namespace _006_类的定义和声明
{
    class Program
    {
        static void Main(string[] args)
        {
            //Vehicle car1 = new Vehicle();
            //car1.speed = 100;
            //car1.Run();
            //car1.Stop();
            //Console.WriteLine(car1.speed);
            //Console.ReadKey();
            //Vector3 v1 = new Vector3();
            //v1.x = 1;
            //v1.y = 1;
            //v1.z = 1;
            //v1.SetX(1);
            //v1.SetY(1);
            //v1.SetZ(1);
        //使用属性
            Vector3 v1 = new Vector3(1,1,1);
            //v1.MyIntProperty = 100;//对属性设置值
            //int temp = v1.MyIntProperty;//对属性取值
            v1.X = 100;//若set 用 private 修饰,set方法不能使用。
            v1.Name = "pengrui";
            Console.WriteLine(v1.Name);
            Console.ReadKey(); 
        }
    }
}

Vector3.cs

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

namespace _006_类的定义和声明
{
    public class Vector3 //设置为public 这样在别的项目中才可以访问
    {
        //编程规范上,习惯上把所有的字段设置为“private”,只可以在类内部访问,不能通过对象访问
        private float x, y, z,length;
        private int age;
        //private string name;
        //public string Name
        //{
        //    get { return name; }
        //    set { name = value;}
        
        //}

        public string Name { get; set; }//编译器会自动给我们提供一个字段,来存储name
      
        public int Age
        {
            set
            {
                //通过set方法 在设置值之前做一些校验的工作 属性的更多好处,需要在写代码过程中体会
                if (value >= 0)
                {
                    age = value;
                }
            }
        }
        public float X
        {
            get { return x; }
            set { x = value; }//如果在get或者set面前加上private,表示这个块只能在类内部调用
        }

        //为字段提供set方法,来设置字段的值
        public void SetX(float x)
        { 
            //如果我们直接在方法内部访问同名的变量的时候,优先访问最近(形参)
            //我们可以通过this.表示访问的是类的字段或者方法
            this.x = x;
        
        }
        public void SetY(float y)
        {
            //如果我们直接在方法内部访问同名的变量的时候,优先访问最近(形参)
            //我们可以通过this.表示访问的是类的字段或者方法
            this.y = y;

        }
        public void SetZ(float z)
        {
            //如果我们直接在方法内部访问同名的变量的时候,优先访问最近(形参)
            //我们可以通过this.表示访问的是类的字段或者方法
            this.z= z;
        }
        public Vector3()
        {
            Console.WriteLine("Vector3的构造函数被调用了");
        }
        public Vector3(float x,float y,float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public float Length()
        {
          return (float)Math.Sqrt(x*x+y*y+z*z);
        }
        //定义属性
        public int MyIntProperty//注意此处没有小括号,加了会被编译器识别为方法,属性的本质是一个字段。
        {
            set
            {
                Console.WriteLine("属性中set块被调用");
                Console.WriteLine("在set块中访问value的值是:"+value);
            
            }
            get//如果没有get块,就不能通过属性取值了
            {
                Console.WriteLine("属性中的get块被调用");
                return 100;
            }
        }
    }
}

Vehicle.cs

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

namespace _006_类的定义和声明
{
    class Vehicle
    {
        public float speed;
        public float maxSpeed;
        public float weight;

        public void Run()
        {
            Console.WriteLine("这个车正在以"+speed+"m/s的速度前行");
        }
        public void Stop()
        {
            speed = 0;
            Console.WriteLine("车辆停止");
        }
    }
}

007-匿名类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _006_类的定义和声明;

namespace _007_匿名类型
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 100;
            var j = 1000;
            var a = "ddfd";//在变量声明时,用val,在变量初始化是,变量类型就被确定下来了。
            var v1 =new Vector3();
		}
    }
}

008-值类型和引用类型-程序运行时的内存占用

源码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _006_类的定义和声明;

namespace _008_值类型和引用类型_程序运行时候内存的占用
{
    //值类型:int、float...struct,
    //引用类型:string 数组 类 等
    class Program
    {
        static void Main(string[] args)
        {
            //Test1();
            //Test2();
            Test3();
            //Test4();
            //Test5();
            Console.ReadKey();
        }
        static void Test1()
        {
            int i = 34;
            int j = 35;
            int temp = 36;
            char c = 'a';
            bool b = true}
        static void Test2()
        {
            int i = 34;
            int j = 35;
            string name = "lidanlan"}
        static void Test3()
        {
            string name1 = "siki";
            string name2 = "taikr";
            name1 = name2;//当我们使用引用类型赋值的时候,其实是赋值的引用类型的应用。
            Console.WriteLine(name1+":"+name2);
        }
        static void Test4()
        {
            Vector3 v = new Vector3();
            v.X = 100;
            v.y = 100;
            v.z = 100; 
            Vector3 v2 = new Vector3();
            v2.X = 200;
            v2.y = 200;
            v2.z = 200;
            v2 = v;
            v2.x = 300;
            Console.WriteLine(v.x);
        }
        static void Test5()
        {
            Vector3[] vArray = new Vector3[] {new Vector3(),new Vector3(),new Vector3()};
            Vector3 v1 = vArray[0];
            vArray[0].x = 100;
            v1.x = 200;
            Console.WriteLine(vArray[0].x);
        }
     }
}

示意图

Test1运行示意图

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

009 面向对象编程-继承

Program.cs

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

namespace _009_面向对象编程_继承
{
    class Program
    {
        static void Main(string[] args)
        {

            //Boss boss = new Boss();
            //boss.Attack() ;//继承:父类里面所有数据成员和函数成员都会继承到子类里面

            //Enemy enemy;
            //enemy = new Boss();//父类声明的对象,可以用子类去构造 子类声明的对象不可以使用父类构造
             enemy虽然使用父类进行了声明,但是使用了子类构造,所有本质上是一个子类类型的,我们可以强制类型转换成子类类型
            //Boss boss = (Boss)enemy;
            //boss.Attack();
            //Enemy enemy = new Enemy();
            //Boss boss = (Boss)enemy;//一个对象是什么类型的 主要看它是通过什么构造的,此行会出现强制类型转换出错。
            //Boss boss = new Boss();
            boss.Attack();
            //boss.AI();//会调用boss move方法。

            //Enemy boss = new Boss();
            //boss.Move();//隐藏方法:如果使用子类声明的对象,调用隐藏方法会调用子类的,如果使用父类声明对象,那么就会调用父类的隐藏方法。
            //Console.ReadKey();
            //Crow crow = new Crow();
            //crow.Fly();
            Bird crow = new Crow();//我们可以通过抽象类去声明对象 但是不可以去构造
            crow.Fly();
            Console.ReadKey();
        }
    }
}
/*笔记
 * 在子类里面重写虚函数之后,不管在哪里调用都是调用重写之后的方法
 
 */

Bird.cs

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

namespace _009_面向对象编程_继承
{
   abstract class Bird //一个抽象类 就是一个不完整的模板
    {
        private float speed;
        public void Eat()
        { 
        
        }
        public abstract void Fly();
    }
}

Boss.cs

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

namespace _009_面向对象编程_继承
{
    class Boss:Enemy
    {
        //public override void Move()//重写:原来的方法不存在了
        //{
        //    Console.WriteLine("这里是Boss的MOve方法");
        //   // base.Move();
        //}
        public new void Move()//当子类里面有一个签名和父类相同的方法的时候,就会把父类中的方法隐藏
        {//隐藏:只是把父类中的方法隐藏了,看不到了,实际这个方法还存在
            Console.WriteLine("这里是Boss的MOve方法");
            // base.Move();
        }
        public void Attack()
        {
            base.Move();//base可以调用父类中的方法和字段,有没有都可以访问,但是加上base.IDE工具会给出提示,把所有可以调用的字段和方法罗列出来方便选择
            this.AI();
            this.Move();
            Console.WriteLine("Boss正在进行攻击");
        }

    }
}

Crow.cs

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

namespace _009_面向对象编程_继承
{
    class Crow:Bird//我们继承了一个抽象类的时候,必须去实现抽象方法
    {
        public override void Fly()
        {
            Console.WriteLine("乌鸦在飞行");
        }
    }
}

Enemy.cs

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

namespace _009_面向对象编程_继承
{
    class Enemy
    {

        private float hp;//血量
        private float speed;

        public float HP
        {
            get { return hp; }
            set { hp = value;}
        }
        public float Speed
        {
            get { return speed; }
            set { speed = value; }
        }
        public void AI()
        {
           // Move();
            Console.WriteLine("这里是Enemy1的公有AI方法");
        
        }
        //public virtual void Move()
        //{
        //    Console.WriteLine("这是Enemy1的公有Move方法");
        
        //}
        public void Move()
        {
            Console.WriteLine("这是Enemy的move方法");
        }
    }
}

Type1Enemy.cs

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

namespace _009_面向对象编程_继承
{
    class Type1Enemy:Enemy
    {
    }
}

Type2Enemy.cs

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

namespace _009_面向对象编程_继承
{
    class Type2Enemy:Enemy
    {
    }
}

010-密封类和密封方法

Progam.cs

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

namespace _010_密封类和密封方法
{
    class Program
    {
        static void Main(string[] args)
        {

			Console.ReadKey();
        }
    }
}

BaseClass.cs

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

namespace _010_密封类和密封方法
{
     //sealed class BaseClass//这里声明了一个密封类
    class BaseClass
    {
        public virtual void Move()
        { 
        }
    }
}

DerivedClass.cs

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

namespace _010_密封类和密封方法
{
    class DerivedClass:BaseClass//sealed密封类无法被继承
    {
        public sealed override void Move()//我们可以把重写的方法声明为密封方法,表示该方法不能被重写
        {
            base.Move();
        }
    }
}

011-派生类的构造函数

Program.cs

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

namespace _011_派生类的构造函数
{
    class Program
    {
        static void Main(string[] args)
        {
           // DerivedClass dr = new DerivedClass(1,2);
            //BaseClass O1 = new BaseClass();
            ClassXYZ.z = 100;
            Console.WriteLine(ClassXYZ.z);
            ClassXYZ.TestMethod();
            Console.ReadKey();
        }
    }
}
/*
 * 1.public 公有的 private私有的
 * 2.在class 前面加 public 才能在别的项目当中访问class,不加class就不行
 * 3.protect 保护的,当没有继承的时候,它的作用private是一样的,当有继承的时候,protect表示被子类访问的字段和方法。
 * 4.static 可以修饰字段或者方法,修饰字段的时候,表示这个字段是静态的数据,叫做静态字段或者静态属性,修饰方法的时候,叫做静态方法,或者静态函数
 *  使用static修饰的成员,只能通过类名访问。
 * 5. 当我们构造对象的时候,对象中只包含了普通的字段,不包含静态字段
 */

BaseClass.cs

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

namespace _011_派生类的构造函数
{
    class BaseClass
    {
        private int x;
        protected int z;
        public BaseClass()
        {
            Console.WriteLine("这里是BaseClass的无参构造函数");
        }
        public BaseClass(int x)
        {
            this.x = x;
            Console.WriteLine("x赋值完成");
        }
    }
}

ClassXYZ.cs

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

namespace _011_派生类的构造函数
{
    class ClassXYZ
    {
        public static void TestMethod()
        {
            Console.WriteLine("这里是静态方法");
        
        }
        public static int z;//静态字段
        private int x;
        private int y;
    }
}

DerivedClass.cs

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

namespace _011_派生类的构造函数
{
    class DerivedClass:BaseClass
    {
        private int y;
       // public DerivedClass():base()//调用父类中无参构造函数,先调用父类的,再调用子类的,如果没有显示调用,会默认调用父类无参构造方法
        public DerivedClass()
         {
             Console.WriteLine("这是DerivedClass的无参构造函数");
         }
        public DerivedClass(int x, int y):base(x)
        {
            this.y = y;
            base.z = 100;
            Console.WriteLine("y赋值完成");
        }
    }
}

012-定义和实现接口

Program.cs

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

namespace _012_定义和实现接口
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Bird.cs

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

namespace _012_定义和实现接口
{
    class Bird:IFly,IB
    {

        public void Fly()
        {}

        public void MethodA()
        {}
        public void Method1()
        { }
        public void Method2()
        { }


    }
}

IA.cs

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

namespace _012_定义和实现接口
{
    interface IA
    {
        void Method1();

    }
}

IB.cs

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

namespace _012_定义和实现接口
{
    interface IB:IA
    {
        void Method2();

    }
}

IFly.cs

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

namespace _012_定义和实现接口
{
    interface IFly
    {
        void Fly();
        void MethodA();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值