unity C# collection

The Collection class is a class specifically used for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interface.

Collection classes serve different purposes, such as dynamically allocating memory for elements, accessing list items based on indexes, etc. These classes create collections of objects of the Object class. In C#, the Object class is the base class for all data types.

Various collection classes and their usage

The following are various commonly used System.Collection namespace classes. Click the link below for details.

kind Description and usage
Dynamic array (ArrayList) It represents an ordered collection of objects that can be individuallyindexed.

It basically replaces an array. However, unlike arrays, where you can add and remove items at specified locations using indexing , a dynamic array will automatically resize it. It also allows dynamic memory allocation, addition, searching, and sorting of items in lists.

Hashtable It useskeys to access elements in the collection.

When you access an element using a key, a hash table is used and you can identify a useful key value. Each item in the hash table has akey/value pair. Keys are used to access items in the collection.

SortedList It can access the list using keys and indexes items in.

A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or index. If you access items using indexes, it's a dynamic array (ArrayList), if you access items using keys, it's a hashtable (Hashtable). Items in a collection are always sorted by key value.

Stack It represents a last-in-first-out collection of objects.

Use a stack when you need last-in-first-out access to items. When you add an item to the list, it is called a push element, and when you remove an item from the list, it is called a Popup element.

Queue It represents a first-in-first-out collection of objects.

Use a queue when you need first-in, first-out access to items. When you add an item to the list, it is called enqueue, and when you remove an item from the list, it is called < a i=3>dequeue.

BitArray It represents abinary array represented using the values ​​1 and 0.

Use a dot array when you need to store bits but don't know the number of bits in advance. You can access items from a collection of point arrays using integer indexes, with indexes starting from zero.

 C# Event

C# Generic

2 notes Write notes

  1.    hopeitdoes

      257***[email protected]

    111

    Here I would like to add something that seems not to be introduced above - List.

    Let’s first look at how to build it:

    var list=new List<int>();

    First of all, List is a strong type and is very safe. Secondly, look at the angle brackets. They are generics added in C# 2.0, so there is no such thing as ArrayList.

    That would require unpacking/packing, resulting in a waste of performance.

    Then, List is allocated by index, which is the same as an array, starting from 0. It can read values ​​by index:

    var a=new List<int>();
    a.Add(12);
    a.Add(10);
    Console.WriteLine(a[0]);

    Lists can have identical items, and items are sorted manually.

    After changing an item, note that the index of the item will change:

    var a=new List<int>();
    a.Add(12);
    a.Add(10);
    Console.WriteLine(a[0]);
    a.Remove(12);
    Console.WriteLine(a[0]);

    Provide some commonly used list methods:

    •  1. Add() adds things to the end of the list.
    •  2. Remove() deletes the first item among the items that matches the condition you want to delete (delete the first item that matches this condition).
    •  3. Clear() clears all items.
    •  4. Sort() sorts items using the system default method.
    •  5. Contains() checks whether an item exists in the list.

    Look at the example:

    using System;
    using static System.Console;
    using System.Collections.Generic;
    namespace HelloWorldApplication
    {
       class HelloWorld
       {
          static void Main(string[] args)
          {
              var a=new List<int>();
              a.Add(2);
              a.Add(6);
              a.Add(2);
              a.Add(10);
             Console.WriteLine($"The first number is {a[0]}");
              a.Remove(2);//Delete the first item matching this condition
              a.Sort();
              foreach(var a2 in a)
              {
                  WriteLine(a2);
              }
              bool a3=a.Contains(2);
              WriteLine(a3);
             Console.ReadKey();
          }
       }
    }
    hopeitdoes

       hopeitdoes

      257***[email protected]

    2 years ago (2022-04-20)
  2.    hopeitdoes

      lxw***[email protected]

    52

    Share how to use the dictionary:

    Let’s first look at how to construct:

    var a=new Dictionary<TKey,TValue>();

    When you see the angle brackets, you know that it is a generic type in C# 2.0, so it can accommodate any type.

    First of all, the dictionary has a key <TKey> and a value <TValue>, where the key must be unique and cannot be repeated .

    Key cannot be a null reference

    Secondly, we can use keys to index instead of index values.

    WriteLine(a[TKey]);

    Come, let’s introduce some commonly used things in dictionaries.

    1 Add(): Add keys and values

    2 Clean(): Clear all keys and values ​​in the dictionary

    3 Count: Get how many pairs of keys and values ​​there are in the dictionary

    4 Remove(): delete a key and value;

    5-6: ContainsKey()/ContainsValue(): Check whether the specified key/value is included;

    Look at the example:

    using System;
    using System.Collections.Generic;
    namespace HelloWorldApplication
    {
       class A
       {
          static void Main(string[] args)
          {
              var a=new Dictionary<int,int>();
              a.Add(12,14);
              a.Add(0,1);
              Console.WriteLine("Count before deletion"+a.Count);
              a.Remove(0);
              Console.WriteLine(a[12]);
              Console.WriteLine(a.Count);
             Console.WriteLine(a.ContainsKey(12));
             Console.ReadKey();
          }
       }
    }

    Finally,dictionaries, stacks, and queues cannot be sorted. If you want to sort the dictionary, you must use other methods or collections, such as SortedDictionary<TKey ,TValue>.

    There are many kinds of automatic sorting, and their performance varies slightly, so carefully choose the best automatic sorting set for your project.

Guess you like

Origin blog.csdn.net/qq_21743659/article/details/134833399