lambda表达式和 delegate,此外还有Func Predicate
static void Main(string[] args)
{
test((float a,string b) =>
{
Console.WriteLine(a);
});
test(delegate(float a,string b)
{
Console.WriteLine(b);
});
}
static void test(Action<float,string> act)
{
act(1.0f, "asdf");
}
- Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型
- Func可以接受0个至16个传入参数,必须具有返回值
- Action可以接受0个至16个传入参数,无返回值
- Predicate只能接受一个传入参数,返回值为bool类型
数据结构
主要的类型有Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue
using System.Collections.Generic
//array
int[,] aa = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
Console.Write(aa[1, 2]);
foreach(int a in aa)
{
Console.Write(a);
}
//--list
List<int> bb = new List<int>();
bb.Add(2);
bb.Insert(0, 5);
int index = bb.Find((int a) => {
return a == 2;
});
Console.Write(index);
bb.Remove(5);
bb.RemoveAt(index);
//---ArrayList 基本和List相同,只是无类型,尽量不要用
//use System.Collections
ArrayList cc = new ArrayList();
cc.Add(5);
cc.Insert(0, null);
cc.Add(new int[5]);
//-------string
string dd = "112233445566";
dd.IndexOf("33");
dd.Substring(2, 3);
dd.Replace("44", "bb");
dd.Insert(0, "aa");
//--------Dictionary
Dictionary<int, string> ee = new Dictionary<int, string>();
ee.Add(5, "5555");
Console.Write(ee[5]);
ee.ContainsKey(5);
string s;
ee.TryGetValue(5, out s);
foreach(KeyValuePair<int, string> e in ee)
{
Console.Write(e);
}
//-----Hashtable 无类型,线程安全,使用不如Dictionary方便
Hashtable ff = Hashtable.Synchronized(new Hashtable());//多线程度,互斥写
ff.Add(1, "aaa");
ff.Add(2, 5);
Console.Write(ff[2]);
foreach (DictionaryEntry f in ff)
{
}
//---------stack
Stack<int> gg = new Stack<int>();
gg.Push(1);
gg.Pop();
//-----queue
Queue<int> hh = new Queue<int>();
hh.Enqueue(1);
hh.Dequeue();