数组&集合

数组包括一维数组和二维数组还有多维数组,其中一维数组可以表示为:

int[] arr1 = new int[2] { 1, 2 };

二位数组可以表示为:

int[,] arr2 = new int[2, 3] {
{0,1,2 },
{2,3,4 }
};

其中三位数组:int[,,] arr2 = new int[1,2,3] (现在用不到)

集合包括 ArrayList通用型:

ArrayList alt = new ArrayList();
alt.Add("123");
alt.Add(123);
alt.Add(true);
bool iscontain = alt.Contains("1");
alt.Clear();
/*alt.Insert(0, "abc")*/
;
Console.WriteLine(iscontain);

for(int i = 0; i < alt.Count; i++)
{
Console.WriteLine(alt[i].ToString() + " " + alt[i].GetType().ToString());
}

foreach (var x in alt)
{
Console.WriteLine(x.ToString() + " " + x.GetType().ToString());
}

其中还有泛型集合 List

泛型集合要规定其类型:

泛型集合 List
List<string> str_list = new List<string>();
str_list.Add("a");
str_list.Add("b");
str_list.Add("c");
str_list.Add("d");

foreach(string x in str_list)
{
Console.WriteLine(x);
}

还有哈希表,哈希表不需要规定类型和键值:

Hashtable ht = new Hashtable();
ht.Add("1", "a");
ht.Add(2, "b");
ht.Add(3, false);
ht.Add("x", 3.14);
Console.WriteLine(ht[2]);
foreach (var x in ht.Keys)
{
Console.WriteLine(ht[x]);
}
Console.ReadLine();

字母表,需要规定键型和类型:


Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("a", 3);
dic.Add("b", 4);
dic.Add("c", 5);
dic.Add("d", 6);
dic.Add("e", 7);

foreach (var x in dic.Keys)
{
Console.WriteLine(dic[x]);
}

还有queue先进先出和stack先进后出:


Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("a", 3);
dic.Add("b", 4);
dic.Add("c", 5);
dic.Add("d", 6);
dic.Add("e", 7);

foreach (var x in dic.Keys)
{
Console.WriteLine(dic[x]);
}

结构体和枚举

struct human
{
public int age;
public bool isman;
public double height;
public double weight;
public string name;
}

enum week
{
星期一,
星期二,
星期三,
星期四,
星期五,
星期六,
星期日
}
static void Main(string[] args)
{
human ck;
ck.age = 30;
ck.isman = true;
ck.height = 180.2;
ck.weight = 80.1;
ck.name = "崔凯";

Console.WriteLine(ck.GetType());

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(week.星期一);

Console.ReadLine();

原文地址:https://www.cnblogs.com/yujiamin123/p/6993858.html