C# basic knowledge learning 1

exception handling

finally block, which is executed regardless of whether an exception occurs

    class Program
    {
        static void Main(string[] args)
        {

            try
            {
                int[] array = { 1, 2, 3, 4 };
                int num = array[4];
            }
            catch
            {
                Console.WriteLine("捕捉到程序出现错误");
            }
            finally
            {

                Console.WriteLine("无论是否出现异常都要走这一步");
            }

            Console.WriteLine("Test");
            Console.ReadKey();
        }
    }

Below is a small case of summation

    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 0, num2 = 0;
            Console.WriteLine("输入数字求和");

            while (true)
            {
                try
                {
                    num1 = Convert.ToInt32(Console.ReadLine()); //如果此行代码出现异常,剩下的代码将不会继续执行
                    break; //输入正确会跳出循环
                }
                catch
                {
                    Console.WriteLine("请输入第一个有效数字");
                }
            }

            while (true)
            {
                try
                {
                    num2 = Convert.ToInt32(Console.ReadLine()); //如果此行代码出现异常,剩下的代码将不会继续执行
                    break;
                }
                catch
                {
                    Console.WriteLine("请输入第二个有效数字");
                }
            }

            int sum = num1 + num2;
            Console.WriteLine("求和:"+sum);
            Console.ReadKey();
        }
    }

Object-Oriented Programming

First you need to create a class

    class Customer
    {
        public string name;
        public string address;
        public int age;
        public string buyTime;

        public void ShowInfo()
        {
            Console.WriteLine("姓名:" + name);
            Console.WriteLine("地址:" + address);
            Console.WriteLine("年龄:" + age);
            Console.WriteLine("购买时间:" + buyTime);
            Console.ReadKey();
        }
    }

Then call this class in the main program

    class Program
    {
        static void Main(string[] args)
        {
            Customer customer;  //声明一个变量(对象)
            customer = new Customer();  //new 使变量初始化
            customer.name = "老高";
            customer.ShowInfo();
        }
    }

Constructor (constructor method)

  1. The constructor is done synchronously when the variable is initialized with new.
  2. When we don't write a constructor, the compiler will automatically provide us with a no-argument constructor. When we define a constructor, the new constructor will overwrite the default one.
  3. The written data will not be overwritten by the data in the constructor
    class Customer
    {
        public string name;
        public string address;
        public int age;
        public string buyTime;

        public Customer()  //构造方法
        {
            name = "默认名字";
            address = "默认地址";
            age = 0;
            buyTime = "默认购买时间";
        }

        public void ShowInfo()
        {
            Console.WriteLine("姓名:" + name);
            Console.WriteLine("地址:" + address);
            Console.WriteLine("年龄:" + age);
            Console.WriteLine("购买时间:" + buyTime);
            Console.ReadKey();
        }
    }

Before and after adding the construction method:

//前
姓名:老高
地址:
年龄:0
购买时间:

//后
姓名:老高
地址:默认地址
年龄:0
购买时间:默认购买时间

Attributes

There are two forms of defining properties:

        public int Age  //普通定义属性的写法
        {
            set
            {
                if(value>=0)  //通过set方法,可以在设置数值之前进行简单的校验
                {
                    age = value;
                }
            }
            get
            {
                return age;
            }
        }
        //调用
        customer.Age = 21;
        public int age { get; set; }  //利用系统定义好的
        //调用
        customer.age = 21;

Output result:

姓名:老高
地址:默认地址
年龄:21
购买时间:默认购买时间

anonymous type

Use var to declare an anonymous type. When initialized, this variable is determined and cannot be modified later.
var var1 =34

Heap and stack (areas of memory when the program is running)

We divide the memory into heap space and stack space. The
stack space is relatively small, but the reading speed is relatively block
. The heap space is relatively large, but the reading speed is relatively slow.

Features of stack:

  • Data can only be inserted and deleted from the top of the stack
  • Putting data on top of the stack is called Push
  • Deleting data from the top of the stack is called popping

write picture description here

The characteristics of the heap: the
heap is a memory area that is not used with the stack. The memory in the heap can be stored and removed in any order.

write picture description here

GC garbage collection mechanism

The GC of the CLR is the memory management mechanism. We do not need to care about the use of memory when writing programs, because these CLRs have already helped us to complete them.

write picture description here

Value Types and Reference Types

There are two types of types, one is value type (integer, bool, struct, char, decimal) and reference type (string array, custom class, built-in class).

Value types only need a separate piece of memory, which is used to store the actual data (on the stack when defined separately).
Reference types need two pieces of memory:

  • The first segment stores the actual data, which is always in the heap
  • The second segment is a reference, which points to the storage location of the data in the heap (the value type is placed on the stack, and the reference part such as string is placed on the stack in the form of an address, and the address corresponds to the data in the heap. When releasing memory, first Release the stack memory. At this time, there are null references in the heap, and the GC recycling mechanism recycles garbage)

write picture description here

class Program
{
    static void Main(string[] args)
    {
        //Test1();
        Test2();
        Console.ReadKey();
    }

    static void Test1()
    {
        string name1 = "Zhangsan11";
        string name2 = "Lisi2";
        name1 = name2;
        name1 = "Wangwu3";
        Console.WriteLine(name1 + "," + name2);
        //输出结果: Wangwu3,Lisi2
        //但使用引用类型赋值的时候,其实是赋值的一个引用
    }

    static void Test2()
    {
        Vector3 v1 = new Vector3();
        v1.x = 100;
        v1.y = 100;
        v1.z = 100;
        Vector3 v2 = new Vector3();
        v2.x = 200;
        v2.y = 200;
        v2.z = 200;
        v2 = v1;  //v1 v2都指向 v1的引用
        v2.x = 300;
        Console.WriteLine(v1.x);
        //输出结果:300
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325765962&siteId=291194637