C#快速入门

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值