Unity C# Basics Review 20 - Generic Collections, Unboxing and Packing (P392)

1. Comparison of arrays and sets

1. The array declares the element type, but the collection does not, because all the elements used in the collection are stored as objects. The size of the array is fixed and cannot be increased or decreased; while the collection class can dynamically adjust the size as needed.

2. The methods of retrieving elements are different.

2. Packing and unpacking

1. Boxing occurs when the value type is converted to a reference type.
object obj =1;
this line of statement assigns the integer constant 1 to the variable obj of the object type. As we all know, the constant 1 is a value
type, and the value type must be placed on the stack. , and object is a reference type, it needs to be placed
on the heap; to place the value type on the heap, a boxing operation needs to be performed.
The above is the operation to be performed for boxing. When performing the boxing operation, it is inevitable to
apply for memory space on the heap and copy the value type data on the stack to the applied heap memory space
. This will definitely consume memory and of cpu resources.
2. Unboxing occurs when the reference type is converted to the value type.
object objValue =4;
int value =(int)objValue;
the above two lines of code will perform a boxing operation and box the integer constant 4 into a reference
type object variable objValue; and then perform an unboxing operation to store the reference variable objValue stored on the heap
into the local integer value type variable value. The execution process of the unboxing operation is exactly the opposite of the boxing operation process. It converts the reference type value
stored on the heap into a value type and assigns it to the value type variable.

3. Packing and unpacking instructions

 4. Test code illustration (compare ArrayList and generic List<T>)

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

namespace Test01
{
    class Student 
    {

    }
    class Teacher 
    {

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

        public static void TestList() 
        {
            //非泛型的情况
            ArrayList stus = new ArrayList();
            stus.Add(new Teacher());     //存入Teacher类
            for (int i = 0; i < stus.Count; i++)
            {
                //Student s = (Student)stus[i];  //强制转成Student类会报错
                //需要写一个判断是否为Student类,是的话才能放入
                if (stus[i] is Student)
                {
                    Student s = (Student)stus[i];  
                }
            }

            //泛型List<>
            List<Student> list = new List<Student>();   //只收Student类型
            //list.Add(new Teacher());   //无法放入,会报错
            list.Add(new Student());


            List<Student> list = new List<Student>();
            list.Add(new Student());
        }

    }
}

1. Collections with generics are used to avoid certain type conversion errors and are specific.

2. If you require the Teacher class and Student class to be put into a collection, you can use ArrayList

Guess you like

Origin blog.csdn.net/weixin_46711336/article/details/124845708