score:94
that is a lambda expression. it is a very special anonymous delegate. basically you are defining a method and not giving a name. its parameters are to the left of the =>
and the method body is to the right of the =>
. in your particular case,
(se, cert, chain, sslerror) => { return true; };
is an anonymous method defined by a lambda expression. this particular method has four parameters
object se
x509certificate cert
x509chain chain
sslpolicyerrors sslerror
and the method body is
return true;
it's as if you had said
class servercertificatevalidation {
public bool onremotecertificatevalidation(
object se,
x509certificate cert,
x509chain chain,
sslpolicyerrors sslerror
) {
return true;
}
}
and then
var validation = new servercertificatevalidation();
system.net.servicepointmanager.servercertificatevalidationcallback +=
validation.onremotecertificatevalidation;
how is that
(blah,blah,blah)=>{return true;}
construct called and where can i find more info on such constructs?
it's called the same way that any other method is called. for example, you can do this:
func<int, int, int> adder = (m, n) => m + n;
here i am defining a method that eats a pair of int
and returns an int
. that int
is obtained by adding the values of the input parameters. it can be invoked like any other method.
int four = adder(2, 2);
here's an article on msdn on lambda expressions and an article on the lambda operator. if you're really interested, the name comes from lambda calculus.
score:1
this snippet is called anonymous function. it builds an anonymous method around the callback delegate and allways returns true.
score:3
jason explains it very well. here is an example using an event that is listened to using different techniques:
using system;
namespace events
{
class program
{
static void main(string[] args)
{
events e = new events();
e.fireevents();
console.readline();
}
}
public class events
{
private event eventhandler<eventargs> eventtest;
public events()
{
eventtest += new eventhandler<eventargs>(function);
eventtest += delegate
{
console.writeline("written by an anonymous method.");
};
eventtest += (o, e) =>
{
console.writeline("written by a lambda expression");
};
}
private void function(object sender, eventargs e)
{
console.writeline("written by a function.");
}
public void fireevents()
{
if (eventtest != null)
eventtest(this, new eventargs());
}
}
}
score:4
(blah,blah,blah)=>{return true;}
is a lambda expression. it doesn't look like the lambdas you're used to because it doesn't use any arguments which get passed to it. the compiler will turn this lambda into a delegate function for you, without you having to go through the long, annoying process of creating a whole function which implements the delegate specification that servicepointmanager.servercertificatevalidationcallback uses.
score:6
it's called a lambda expression.
http://msdn.microsoft.com/en-us/library/bb311046.aspx - the lambda operator.
score:6
the =>
-operator represents a lambda expression.
but for those of you who visit the question nowadays, another use-case might be the arrow as a shorthand for a property getter. this feature got introduced in c# 6. so instead of writing
public string foo
{
get
{
return this.bar;
}
}
you can use following snippet:
public string foo
{
get => this.bar;
}
or even shorter:
public string foo => this.bar;
score:19
for completeness (for search results, etc): in more recent versions of c# (since 6.0), the =>
syntax has been extended from just lambdas for delegates and expression trees, to cover expression-bodied members. this means that a range of simple members such as properties, methods, etc - can be implemented as expression bodies; for example:
public int foo { get { return innerobj.someprop; } }
public void bar() { write("thing"); }
can be written:
public int foo => innerobj.someprop;
public void bar() => write("thing");
Source: stackoverflow.com
Related Query
- What does this C# code with an "arrow" mean and how is it called?
- What does Any() mean in this LINQ query?
- How does this linq code that splits a sequence work?
- What does this mean in C# or LINQ? - ( () => )
- How does coding with LINQ work? What happens behind the scenes?
- LINQ with Where and All not filtering data - What is wrong with this query?
- What is LINQ? How can I use this with PHP?
- what does this .net line of code means
- What does it mean when dotpeek fills a method with "// Stub method ('ret' instruction only)"
- Need advice on how to best deal with the storing/unboxing of different types in a list and what is more optimal in my case
- how could i handle this linq query and compare it with and int in an if-statement?
- CS0176 Compiler error. What does it mean an how do I solve it
- How to rewrite this LINQ query with SQL syntax and <>?
- How to Implement this Sql block contain (Having , Join and Group By) with Linq
- How to bind and save an object containing a list of objects to database context in ASP.NET MVC with EF code first?
- What is proper way to write this linq lambda expression and deal with nulls?
- C# XML - Why does this Code keep failing with 0x3A Error?
- Is it possible, and how to refactor this with lambda linq
- Why does this LINQ statement return null and not a IEnumerable with count=0
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- What is LINQ and what does it do?
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- What are Expression Trees, how do you use them, and why would you use them?
- How does LINQ expression syntax work with Include() for eager loading
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- LINQ to Entities does not recognize the method 'System.DateTime AddSeconds(Double)' method, and this method cannot be translated into
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- What does "Replace with single call to single" mean?
- How to insert a record with LINQ and C# and return the Primary Key of that record
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
More Query from same tag
- Correct query linq
- Return a list from Table-Valued SQL Function in LINQ?
- "System.InvalidCastException" error when retrieving data from SQL Server 2012 Express
- Parallel LINQ in WebApps?
- Is there any efficient way to optimize following Linq
- Linq or string filter
- Linq error when read invalid chars
- Delete multiple XML nodes in C# using Linq
- Using Moq .GetMock to register ~1IRepository with Linq Expression?
- How can I use the MYSQL BIT_COUNT function through nhibernate/fluent nHibernate?
- LINQ ThenBy in circle
- C# How do you query a subset of a Dictionary's entries based on the Value's type using Linq?
- .NET List<T> Concat vs AddRange
- Expressions - how to reuse business logic? How to combine them?
- retrieve an image from database using linQ by asp.net
- LINQ with IN clause with exact matches
- How to use a new select on the attribute of the first select?
- SQL Virtual Column in Linq
- Writing Recursive CTE using Entity Framework Fluent syntax or Inline syntax
- c# copying existing XML document with Linq to XML
- CopyToDataTable in LINQ
- select master based on detail in LINQ
- LINQ compound select problem
- Shouldn't calling Enumerable.AsEnumerable() on null variable throw an exception?
- LINQ equivalent of a join query;
- IEqualityComparer with Linq to XML and Distinct() is not executed in code?
- LINQ to SQL in and not in
- Initializing Lookup<int, string>
- get sublist from list, and build average, with linq
- How do i use an Expression<Func<T>> in a linq statement if my navigation property is a single item?