C# 如何使用/理解lambda表达式?
我有以下方法:C# 如何使用/理解lambda表达式?,c#,lambda,C#,Lambda,我有以下方法: private byte[] GetEmailAsBytes(string lstrBody) { byte[] lbytBody; ASCIIEncoding lASCIIEncoding = new ASCIIEncoding(); lbytBody = lASCIIEncoding.GetBytes(lstrBody); return lbytBody; } 我想知道这是否可以转换为lambda表达式。我是新来的。我试过: Func<stri
private byte[] GetEmailAsBytes(string lstrBody)
{
byte[] lbytBody;
ASCIIEncoding lASCIIEncoding = new ASCIIEncoding();
lbytBody = lASCIIEncoding.GetBytes(lstrBody);
return lbytBody;
}
我想知道这是否可以转换为lambda表达式。我是新来的。我试过:
Func<string> BodyToBytes = x => {
ASCIIEncoding lASCIIEncoding = new ASCIIEncoding();
return lASCIIEncoding.GetBytes(x);
}
但这并不适用于编译。我只是想将字符串转换为一系列字节,出于兴趣,我想使用lambda表达式来实现这一点。表达式Func相当于一个不接受参数并返回字符串的函数
您的示例显然返回一个字节[],但您希望它接受一个字符串并返回一个字节[]
要解决此问题,请将BodyToBytes更改为与以下内容匹配。请注意,参数的类型首先是逗号分隔的,然后是返回类型。在这种情况下,x将是string类型
有关引用,请参阅或。表达式Func相当于不接受任何参数并返回字符串的函数
您的示例显然返回一个字节[],但您希望它接受一个字符串并返回一个字节[]
要解决此问题,请将BodyToBytes更改为与以下内容匹配。请注意,参数的类型首先是逗号分隔的,然后是返回类型。在这种情况下,x将是string类型
如需参考,请参阅或。我已编写了一个NUnit示例,以供个人理解
private class ld
{
public Boolean make<T>(T param, Func<T, bool> func)
{
return func(param);
}
}
private class box
{
public Boolean GetTrue() { return true; }
public int Secret = 5;
}
[Test]
public void shouldDemonstrateLambdaExpressions()
{
//Normal Boolean Statement with integer
int a = 5;
Assert.IsTrue(a == 5);
Assert.IsFalse(a == 4);
//Boolean Statement Expressed Via Simple Lambda Expression
Func<int, bool> myFunc = x => x == 5;
Assert.IsTrue(myFunc(5));
Assert.IsFalse(myFunc(4));
//Encapsuled Lambda Expression Called on Integer By Generic Class with integer
ld t = new ld();
Assert.IsTrue(t.make<int>(5,myFunc));
Assert.IsFalse(t.make<int>(4, myFunc));
//Encapsuled Lambda Expression Called on Integer By Generic Class with implicit Generics
Assert.IsTrue(t.make(5, myFunc));
//Simple Lambda Expression Called on Integer By Generic Class with implicit Generic
Assert.IsTrue(t.make(20, (x => x == 20)));
Assert.IsTrue(t.make(20, (x => x > 12)));
Assert.IsTrue(t.make(20, (x => x < 100)));
Assert.IsTrue(t.make(20, (x => true)));
//Simple Lambda Expression Called on a Class By Generic Class with implicit Generic
//FULL LAMBDA POWER REACHED
box b = new box();
Assert.IsTrue(t.make(b, (x => x.GetTrue())));
Assert.IsTrue(t.make(b, (x => x.Secret == 5)));
Assert.IsFalse(t.make(b, (x => x.Secret == 4)));
}
为了我个人的理解,我写了一个NUnit的例子
private class ld
{
public Boolean make<T>(T param, Func<T, bool> func)
{
return func(param);
}
}
private class box
{
public Boolean GetTrue() { return true; }
public int Secret = 5;
}
[Test]
public void shouldDemonstrateLambdaExpressions()
{
//Normal Boolean Statement with integer
int a = 5;
Assert.IsTrue(a == 5);
Assert.IsFalse(a == 4);
//Boolean Statement Expressed Via Simple Lambda Expression
Func<int, bool> myFunc = x => x == 5;
Assert.IsTrue(myFunc(5));
Assert.IsFalse(myFunc(4));
//Encapsuled Lambda Expression Called on Integer By Generic Class with integer
ld t = new ld();
Assert.IsTrue(t.make<int>(5,myFunc));
Assert.IsFalse(t.make<int>(4, myFunc));
//Encapsuled Lambda Expression Called on Integer By Generic Class with implicit Generics
Assert.IsTrue(t.make(5, myFunc));
//Simple Lambda Expression Called on Integer By Generic Class with implicit Generic
Assert.IsTrue(t.make(20, (x => x == 20)));
Assert.IsTrue(t.make(20, (x => x > 12)));
Assert.IsTrue(t.make(20, (x => x < 100)));
Assert.IsTrue(t.make(20, (x => true)));
//Simple Lambda Expression Called on a Class By Generic Class with implicit Generic
//FULL LAMBDA POWER REACHED
box b = new box();
Assert.IsTrue(t.make(b, (x => x.GetTrue())));
Assert.IsTrue(t.make(b, (x => x.Secret == 5)));
Assert.IsFalse(t.make(b, (x => x.Secret == 4)));
}