I'm reviewing another person's code and can't ask him... What does t开发者_开发知识库hat line of C# code do?
It's a lambda expression that takes no parameter and returns true. The equivalent anonymous delegate is:
delegate() { return true; };
Most likely the method requires a parameter of type Func<bool>
, which is a generic delegate with the same signature as above code.
EDIT:
As noted by cdhowie, the above lambda can also be evaluated to an expression tree of type Expression<Func<bool>>
.
Here's the equivalent expression tree:
var body = Expression.Constant(true);
Expression<Func<bool>> returnsTrueExpression = Expression.Lambda<Func<bool>>(body);
You can 'convert' it to an actual delegate by calling Compile()
Func<bool> returnsTrueFunc = returnsTrueExpression.Compile();
This is a lambda expression that always return true. This is a delegate that has functionality similar to this method
bool Test(){
return true;
}
The code reminds me of when you may want to stub or mock values in the early stages of creating unit tests.
You could have a class that has a method like:
bool result = SomeClass.RunSomeImportantProcess(Func<bool> process)
and in your tests you might want to do something as a stub like
Assert.IsTrue(SomeClass.RunSomeImportantProcess(() => true));
Its a lambda expression which doesn't take any parameter but returns true. Whether its required or not, you would have to provide more info to determine.
精彩评论