C#里面=>的含义和用法我没有发现一本C#书介绍这个操作符的用法,越详细越好.// Get the ButtonGrid id var buttongrid = dataContext.RETAILBUTTONGRID.First(c => (string.Compare(c.NAME,gridName,true) == 0));
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/21 17:31:11
C#里面=>的含义和用法我没有发现一本C#书介绍这个操作符的用法,越详细越好.// Get the ButtonGrid id var buttongrid = dataContext.RETAILBUTTONGRID.First(c => (string.Compare(c.NAME,gridName,true) == 0));
C#里面=>的含义和用法
我没有发现一本C#书介绍这个操作符的用法,越详细越好.// Get the ButtonGrid id var buttongrid = dataContext.RETAILBUTTONGRID.First(c => (string.Compare(c.NAME,gridName,true) == 0));
C#里面=>的含义和用法我没有发现一本C#书介绍这个操作符的用法,越详细越好.// Get the ButtonGrid id var buttongrid = dataContext.RETAILBUTTONGRID.First(c => (string.Compare(c.NAME,gridName,true) == 0));
声明一个匿名方法(Lambda表达式)的语法.
没有什么特殊的“越详细越好”用法,大概就是这样
using System;
class Program
{
static void Main(string[] args)
{
Func f1 = x => x + 1;
Func f2 = (x,y) => x + y;
Func f3 = (x,y) => x > y;
Func f4 = x => int.Parse(x);
Console.WriteLine(f1(3));
Console.WriteLine(f2(3,4));
Console.WriteLine(f3(1,2));
Console.WriteLine(f4("123"));
return;
}
}
在框架设计里应用,可以让自己的框架更灵活,比如冒泡排序,
using System;
class Program
{
static void BubbleSort(T[] arr,Func lesser)
where T:class
{
int len = arr.Length;
for (int i = 0; i < len; ++i)
for (int j = len - 1; j > i; --j)
if (lesser(arr[j],arr[i]) == true)
{
T t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
class MyStru
{
public int a;
public int b;
public MyStru(int pa,int pb) { a = pa; b = pb; }
public void Print() { Console.WriteLine("a={0},b={1}",a,b); }
}
static void Main(string[] args)
{
MyStru[] a = new MyStru[5];
a[0] = new MyStru(1,2);
a[1] = new MyStru(3,4);
a[2] = new MyStru(7,8);
a[3] = new MyStru(6,3);
a[4] = new MyStru(2,5);
BubbleSort(a,(x,y) => x.a < y.a);
foreach(var x in a) x.Print();
Console.WriteLine();
BubbleSort(a,(x,y) => x.b < y.b);
foreach(var x in a) x.Print();
Console.WriteLine();
return;
}
}
这样就可以很容易让调用者决定如何比较要排序的类型的大小