Loading...
 
JoiWiki » Developer » .Net » .Net Theory » Lambda Shortcuts Lambda Shortcuts

Lambda Shortcuts

 

I'll write some more on Lambda expressions sometime soon but they're a great way of declaring a method body on the fly which can be lovely when used in conjunction with Delegates. Initially here are some fun things that you can do with them:

 

 

Shorten Pass-Through Method Definitions

Depending on how you're designing your objects, to adhere to good encapsulation principles you'll want to hide any objects that a class has and expose their functionality through public methods on your class which means you'll have a lot of methods and functions which look like this:

public int CountList()
{
return ListObject.Count();
}

public bool ProcessChange(object _ParamObject)
{
return PrivateObj.ProcessChange(_ParamObject);
}

 

Which is all well and good but it takes up a lot of space for something that has a very simple function. A more succinct solution would be to re-write the above using lambda expression syntax:

public int CountList() => ListObject.Count();

public bool ProcessChange(object _ParamObject) => PrivateObj.ProcessChange(_ParamObject);


 

 

 

 

 

 

 

 

 

 

 

Created by JBaker. Last Modification: Tuesday September 24, 2019 11:06:24 BST by JBaker.

Developer