1、要点
2、代码
范型
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstSpell : MonoBehaviour
{
string[] magics = new string[1];
// Start is called before the first frame update
void Start()
{
magics[0] = "临";
magics = AddElement<string>(magics, "兵");
print(magics[1]);
}
// Update is called once per frame
void Update()
{
}
Type[] AddElement<Type>(Type[] array, Type newElement){
Type[]newArray = new Type[array.Length + 1];
for(int i = 0;i< array.Length; i++){
newArray[i] = array[i];
}
newArray[array.Length] = newElement;
return newArray;
}
}
不通过赋值,而是通过引用的形式
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstSpell : MonoBehaviour
{
string[] magics = new string[1];
// Start is called before the first frame update
void Start()
{
magics[0] = "临";
AddElement<string>(ref magics, "兵");
print(magics[1]);
}
// Update is called once per frame
void Update()
{
}
void AddElement<Type>(ref Type[] array, Type newElement){
Type[]newArray = new Type[array.Length + 1];
for(int i = 0;i< array.Length; i++){
newArray[i] = array[i];
}
newArray[array.Length] = newElement;
array = newArray;
}
}
范型集合
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstSpell : MonoBehaviour
{
//list 范型容器
List<string> magics = new List<string>();
//Dictionary 范型字典
Dictionary<string,string> playerInfo = new Dictionary<string,string>();
// Start is called before the first frame update
void Start()
{
//添加新因子or //插入,清空,截取
magics.Add("临");
playerInfo.Add("name","Sophie");
print(playerInfo["name"]);
foreach (var item in playerInfo)
{
print(item.Key + " " +item.Value);
}
}
// Update is called once per frame
void Update()
{
}
}