Lambda Expressions make things even easier by allowing you to avoid the anonymous method and that annoying statement block:
class Program
{
static void Main(string[] args)
{
List
names.Add("Dave");
names.Add("John");
names.Add("Abe");
names.Add("Barney");
names.Add("Chuck");
string abe = names.Find((string name)
=> name.Equals("Abe"));
Console.WriteLine(abe);
}
}
Because Lambda Expressions are smart enough to infer variable types, I don't even have to explicity mention that name is a string above. I can remove it for even more simplicity and write it as such:
class Program
{
static void Main(string[] args)
{
List
names.Add("Dave");
names.Add("John");
names.Add("Abe");
names.Add("Barney");
names.Add("Chuck");
string abe = names.Find(name =>
name.Equals("Abe"));
Console.WriteLine(abe);
}
}
Now there is no particular reason why I have to use name as my variable. Often developers will use 1 character variable names in Lambda Expressions just to keep things short. Here we replace name with p and all works the same.
class Program
{
static void Main(string[] args)
{
List
names.Add("Dave");
names.Add("John");
names.Add("Abe");
names.Add("Barney");
names.Add("Chuck");
string abe = names.Find(p => p.Equals("Abe"));
Console.WriteLine(abe);
}
}
Lambda Expressions Using a Customer Class
I used a list of strings above, but you can just as easily use a list of objects to do the same thing. I will take an abbreviated form of a Customer Class:
public class Customer
{
public int Id;
public string Name;
public string City;
public Customer(int id, string name, string city)
{
Id = id;
Name = name;
City = city;
}
}
and now use Lambda Expressions to find a particular Customer with a name of "Abe".
class Program
{
static void Main(string[] args)
{
List
customers.Add(new Customer(1,"Dave","Sarasota"));
customers.Add(new Customer(2,"John","Tampa"));
customers.Add(new Customer(3,"Abe","Miami"));
Customer abe = customers.Find(c =>
c.Name.Equals("Abe"));
}
}
Again, we could make things more obvious by explicity saying that c is of type Customer, but I wouldn't bet on many people doing it :) You could write the above as follows:
class Program
{
static void Main(string[] args)
{
List
customers.Add(new Customer(1,"Dave","Sarasota"));
customers.Add(new Customer(2,"John","Tampa"));
customers.Add(new Customer(3,"Abe","Miami"));
Customer abe = customers.Find((Customer c) =>
c.Name.Equals("Abe"));
}
}
Combining Extension Methods and Lambda Expressions for Help In Finding Our Customer
Let's say we have a custom CustomerCollection Class not within our control that doesn't provide us an easy way to find a Customer:
public class CustomerCollection :
IEnumerable
{
IEnumerable
public CustomerCollection(IEnumerable
customers)
{
_customers = customers;
}
public IEnumerator
{
foreach (Customer customer in _customers)
yield return customer;
}
System.Collections.IEnumerator System.Collections
.IEnumerable.GetEnumerator()
{
return _customers.GetEnumerator();
}
}
Let's add an Extension Method to the CustomerCollection Class, called GetCustomer:
public static class CustomerExtensions
{
public static Customer GetCustomer(this
CustomerCollection customers,
Predicate
{
foreach (Customer customer in customers)
if (isMatch(customer))
return customer;
return null;
}
}
And now, we can use the CustomerCollection Class to easily find Abe like:
class Program
{
static void Main(string[] args)
{
CustomerCollection collection = GetCustomers();
// Using my GetCustomer Method Extension...
Customer abe = collection.GetCustomer(c =>
c.Name.Equals("Abe"));
}
// Pretend We Don't See This :)
static CustomerCollection GetCustomers()
{
List
customers.Add(new Customer(1,"Dave","Sarasota"));
customers.Add(new Customer(2,"John","Tampa"));
customers.Add(new Customer(3,"Abe","Miami"));
return new CustomerCollection(customers);
}
}
This Is LINQ!But, of course, since CustomerCollection implements IEnumerable
class Program
{
static void Main(string[] args)
{
CustomerCollection collection = GetCustomers();
// LINQ
var abe = collection.Single(c =>
c.Name.Equals("Abe"));
}
// Pretend We Don't See This :)
static CustomerCollection GetCustomers()
{
List
customers.Add(new Customer(1,"Dave","Sarasota"));
customers.Add(new Customer(2,"John","Tampa"));
customers.Add(new Customer(3,"Abe","Miami"));
return new CustomerCollection(customers);
}
}
We collect videos & images from online sites like youtube and some other websites which provide them. And most of the News is also gathered from other online websites. Any material downloaded or otherwise obtained through the use of the service is done at your own discretion and risk and that you will be solely responsible for any damage to your computer system or loss of data that results from the download of any such material.
If you feel that the content on the site is illegal or Privacy please contact us at srinuchalla@gmail.com and such videos, images or any Content will be removed with immediate effect.
No comments:
Post a Comment