<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tag 'Lambda'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=Lambda&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tag 'Lambda'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Lambda Expressions: Execution</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/16/lambda-expressions-execution.aspx</link><pubDate>Fri, 16 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1732784</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;Lambda expressions can be assigned to a delegate variable. The lambda expression is then executed when the delegate is called.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;In the following example, a lambda expression is assigned to a variable (f).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Func&amp;lt;int,string&amp;gt; f = &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;x =&amp;gt; (x + x).ToString();      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(f(5));      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(f(10));&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim f = Function(x as Integer) &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;(x + x).ToString()      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(f(5))      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Debug.WriteLine(f(10))&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The lambda expression in this example is a Func&amp;lt;int, string&amp;gt; [Func(Of Integer, String) in VB] meaning it takes one integer input parameter and returns a string. The lambda expression simply takes the input value, adds it to itself, and returns the result as a string.&lt;/p&gt;  &lt;p&gt;[For more information on Func delegates, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-func-delegates.aspx"&gt;this post&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;When the expression is defined, it is &lt;em&gt;not&lt;/em&gt; executed. It is not executed until it is called.&lt;/p&gt;  &lt;p&gt;The first Debug.WriteLine statement calls the lambda expression, passing in a 5. The result displays &amp;quot;10&amp;quot; in the debug window.&lt;/p&gt;  &lt;p&gt;The second Debug.WriteLine statement calls the lambda expression again, passing in a 10. The result displays &amp;quot;20&amp;quot; in the debug window.&lt;/p&gt;  &lt;p&gt;Lambda expressions are executed when they are called, not when they are constructed. This is important to consider, especially when the lambda expression contains local variables. &lt;/p&gt;  &lt;p&gt;Local variables used in a lambda expression are “captured” or “lifted”. The variable value used is the value at &lt;em&gt;execution&lt;/em&gt; time. The variable lifetime extends to the lifetime of the delegate.&lt;/p&gt;  &lt;p&gt;The following code updates the original example to use a local variable.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;int y = 0;      &lt;br /&gt;Func&amp;lt;int,string&amp;gt; f = x =&amp;gt; (x + y).ToString();       &lt;br /&gt;y = 10;       &lt;br /&gt;Debug.WriteLine(f(5));&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim y As Integer = 0      &lt;br /&gt;Dim f = Function(x) (x + y).ToString()       &lt;br /&gt;y = 10       &lt;br /&gt;Debug.WriteLine(f(5))&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The lambda expression in this example takes the input value, adds it to the current value of the local variable (y), and returns the result as a string.&lt;/p&gt;  &lt;p&gt;The Debug.WriteLine statement in this example calls the lambda expression, passing in a 5.&amp;#160; At the point of executing the Debug statement, y is 10, so the result displays &amp;quot;15&amp;quot; in the debug window.&lt;/p&gt;  &lt;p&gt;For another example of using lambda expressions with local variables, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/02/lambda-expressions-finding-differences-in-two-lists.aspx"&gt;this post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Delegates</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/delegates.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731685</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;A delegate is an object that holds a reference to a method with a particular parameter list and return type.&lt;/p&gt;  &lt;p&gt;It is basically like an object-oriented, type-safe function pointer.&lt;/p&gt;  &lt;p&gt;Delegates basically make it possible to treat methods as entities. You can then assign a delegate to a variable or pass it as a parameter. &lt;/p&gt;  &lt;p&gt;The classic delegate example uses events.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;HelloButton.Click += new EventHandler(HelloButton_Click);&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Or the short-cut version of this code:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;HelloButton.Click += HelloButton_Click;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This passes the HelloButton_Click method to a new EventHandler delegate.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;AddHandler HelloButton.Click, AddressOf HelloButton_Click&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In VB, the AddressOf assigns the address of the HelloButton_Click method to the delegate.&lt;/p&gt;  &lt;p&gt;Instead of using a named method for the delegate (in this example, a HelloButton_Click method), you can use a lambda expression.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;HelloButton.Click += &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;(s, ev) =&amp;gt;      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MessageBox.Show(&amp;quot;Hello World&amp;quot;);&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;AddHandler HelloButton.Click, &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;Function(s, ev) _      &lt;br /&gt;&lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MessageBox.Show(&amp;quot;Hello World&amp;quot;)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In this case the lambda expression has two parameters: s (sender) and ev (eventArgs). When the Click event is generated on the HelloButton, the code displays the MessageBox.&lt;/p&gt;  &lt;p&gt;You can use a lambda expression (inline function) anywhere a delegate is required. However, if the function requires more than a few lines of code, a named method is the preferred and recommended technique.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Func Delegates</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-func-delegates.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731723</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;A Func delegate encapsulates a method that returns a value. It takes up to four parameters (and this number is increased in .NET 4.0) plus the return value.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;The following signature is a Func delegate that takes two integer parameters and returns a string.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Func&amp;lt;int, int, string&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Func(Of Integer, Integer, String)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;There are many examples of Func delegates. &lt;/p&gt;  &lt;p&gt;The following code demonstrates the &lt;strong&gt;Sum&lt;/strong&gt; function that sums the sales total for all customers in the list (and assumes that a Customer object has a SalesTotal property).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;var total = custList.Sum(c =&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.SalesTotal);&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Dim total = custList.Sum(Function(c) _    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.SalesTotal)&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Action Delegates</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-action-delegates.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731722</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;Just as the name implies, an action delegate encapsulates a method that performs an action and has no return value. It takes up to four parameters (and this number is increased in .NET 4.0).&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;ForEach&lt;/strong&gt; method of the List&amp;lt;T&amp;gt; is an example of an action delegate. It takes an action delegate as a parameter.&lt;/p&gt;  &lt;p&gt;One of the common uses of ForEach is to display all of the elements in a list using one line of code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;custList.ForEach(c =&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Debug.WriteLine(c.LastName));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;custList.ForEach(AddressOf WriteToDebug)&lt;/p&gt;  &lt;p&gt;NOTE: VB lambda expressions do not currently support action delegates. In the above example, the code uses a named method as the action delegate. VB 10 lambda expressions will support action delegates using the Sub keyword instead of the Function keyword.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Predicate Delegates</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-predicate-delegates.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731716</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;According to &lt;a href="http://en.wikipedia.org/wiki/Lambda_calculus"&gt;Wikipedia&lt;/a&gt;, a predicate is “a function which returns a Boolean value”.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;A predicate delegate encapsulates a method that evaluates to True or False. It takes a single parameter.&lt;/p&gt;  &lt;p&gt;The Array class Find method is an example of a predicate delegate. It takes an array as a first parameter and a predicate delegate as its second parameter.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;var foundCustomer = Array.Find(custArray, c =&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.LastName.StartsWith(&amp;quot;K&amp;quot;));&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Dim foundCustomer = Array.Find(custArray, Function(c) _    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.LastName.StartsWith(&amp;quot;K&amp;quot;))&lt;/p&gt;  &lt;p&gt;This code iterates through the array, checking each entry and evaluating the expression as true or false.&amp;#160; If the expression is false, it continues. If the expression is true, it returns the entry. Basically, this code finds the first customer in the array with a last name that starts with the defined letter.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Syntax</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-syntax.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731696</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;This post covers the syntax for lambda expressions. It uses the customer list defined in &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx"&gt;this prior post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;The first example below uses the FirstOrDefault extension method on the Enumerable object to find the first customer that matches the defined expression. The FirstOrDefault method takes a delegate as a parameter and that delegate is defined with a lambda expression.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; General syntax:      &lt;br /&gt;Customer foundCustomer =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; custList.FirstOrDefault((Customer c) =&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerId == 4);)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; With inferred typing:      &lt;br /&gt;var foundCustomer =       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; custList.FirstOrDefault(c =&amp;gt; c.CustomerId == 4);)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The lambda expression in this example is as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;c =&amp;gt; c.CustomerId == 4&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The code begins with the set of parameters to the lambda expression. In this example, there is one parameter (c). The =&amp;gt; is the “goes to” or lambda operator. The remainder of the code is the expression itself. In this case, checking for the item in the list where CustomerId is 4.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; General syntax:      &lt;br /&gt;Dim foundCustomer as Customer = _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; custList.FirstOrDefault(Function(c as Customer) _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerId = 4)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; With inferred typing:      &lt;br /&gt;Dim foundCustomer = _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; custList.FirstOrDefault(Function(c) _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerId = 4)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The lambda expression in this example is as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Function(c) c.CustomerId = 4&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The code begins with the word “Function” along with the set of parameters to the lambda expression. In this example, there is one parameter (c). The remainder of the code is the expression itself. In this case, checking the item in the list where CustomerId is 4.&lt;/p&gt;  &lt;p&gt;In either language, notice the shorter syntax with inferred typing. The compiler recognizes that the list contains customers, so it can infer the type of the parameter and the type of return value.&lt;/p&gt;  &lt;p&gt;You can combine some of the extension methods to form more complex statements.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;var query = custList.FindAll(c =&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; c.LastName.StartsWith(“K”)).OrderBy(c =&amp;gt; c.FirstName));&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim query = custList.FindAll(Function(c) _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.LastName.StartsWith(“K”)). _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;OrderBy(Function(c) c.FirstName))&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This code finds all of the entries that match the first lambda expression, basically all customers with a last name that begins with “K”. The code then performs an OrderBy using the second lambda expression, basically sorting the resulting list by first name.&lt;/p&gt;  &lt;p&gt;The above examples demonstrate single-line lambdas. You can also use multi-line lambdas as shown in the example below.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;var foundCustomer =&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#65402e" face="Consolas"&gt;custList.FirstOrDefault(c =&amp;gt;      &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160; Debug.WriteLine(c.LastName);       &lt;br /&gt;&amp;#160;&amp;#160; if (c.CustomerId == 4)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;       &lt;br /&gt;&amp;#160;&amp;#160; else       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;       &lt;br /&gt;});&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Not available in VB9, coming in VB10.&lt;/p&gt;  &lt;p&gt;Multi-line lambda expressions require the standard {} syntax to define the statement block. Whereas single-line lambda expressions automatically handle&amp;#160; the return value for you, you have to handle it yourself in multi-line lambda expressions.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: Finding an Item in a Generic List</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-finding-an-item-in-a-generic-list.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731676</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;In &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx"&gt;this prior post&lt;/a&gt;, I detailed how to build lists of things using a generic List&amp;lt;T&amp;gt;. Once you have a list, you may need to find items in the list. There are several ways to accomplish this, but using lambda expressions is the most concise.&lt;/p&gt;  &lt;p&gt;[To begin with an overview of lambda expressions, &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx"&gt;start here&lt;/a&gt;.]&lt;/p&gt;  &lt;p&gt;For example, here is how you find an item in a generic list using a for/each statement.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Customer foundCustomer = null;      &lt;br /&gt;foreach (var c in custList)       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (c.CustomerId == 4)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foundCustomer = c;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;}       &lt;br /&gt;Debug.WriteLine(foundCustomer);&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim foundCustomer As Customer &lt;/font&gt;&lt;font color="#65402e"&gt;&lt;font face="Consolas"&gt;= Nothing        &lt;br /&gt;For Each c As Customer In custList         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; If c.CustomerId = 4 Then         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foundCustomer = c         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Exit For         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; End If         &lt;br /&gt;Next         &lt;br /&gt;&lt;/font&gt;&lt;font face="Consolas"&gt;Debug.WriteLine(foundCustomer)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Notice the amount of code required to loop through each item in the list and find the one with a particular customer Id.&lt;/p&gt;  &lt;p&gt;You may have heard about language integrated query (LINQ) and how you can use it to search a&amp;#160; list.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Customer foundCustomer = null;      &lt;br /&gt;var query = from c in custList       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where c.CustomerId == 4       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select c;       &lt;br /&gt;foundCustomer = query.FirstOrDefault();       &lt;br /&gt;Debug.WriteLine(foundCustomer);&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim foundCustomer As Customer = Nothing      &lt;br /&gt;Dim query = From c As Customer In custList _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Where c.CustomerId = 4 _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Select c       &lt;br /&gt;foundCustomer = query.FirstOrDefault()       &lt;br /&gt;Debug.WriteLine(foundCustomer)&lt;/font&gt; &lt;/p&gt;  &lt;h6&gt;&lt;font face="Consolas"&gt;&lt;/font&gt;&lt;/h6&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;So LINQ is cool, but the code does not seem much shorter.&lt;/p&gt;  &lt;p&gt;Now let’s look at using lambda expressions to find the item in the list.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt; &lt;font color="#65402e" face="Consolas"&gt;Customer foundCustomer = null;    &lt;br /&gt;foundCustomer = custList.FirstOrDefault(c =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerId == 4);     &lt;br /&gt;Debug.WriteLine(foundCustomer);&lt;/font&gt;   &lt;p&gt;The lambda expression syntax in C# looks like this:&lt;/p&gt; c =&amp;gt; c.CustomerId == 4   &lt;p&gt;The code begins with the set of parameters to the lambda expression. The =&amp;gt; is the “goes to” or lambda operator. The remainder of the code is the expression itself. In this case, checking for the item in the list where CustomerId is 4.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Dim foundCustomer As Customer = Nothing      &lt;br /&gt;foundCustomer = custList.FirstOrDefault(Function(c)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c.CustomerId = 4)       &lt;br /&gt;Debug.WriteLine(foundCustomer)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;The lambda expression syntax in VB looks like this:&lt;/p&gt; Function(c) c.CustomerId = 4   &lt;p&gt;The code begins with the word “Function” along with the set of parameters to the lambda expression. The remainder of the code is the expression itself. In this case, checking the item in the list where CustomerId is 4.&lt;/p&gt;  &lt;p&gt;.NET 3.5 added a large list of extension methods on the Enumerable class. Any object that implements IEnumerable or IEnumerable&amp;lt;T&amp;gt; (basically any object you can do a for/each over) can use these methods. Many of these extension methods (such as the &lt;strong&gt;FirstOrDefault&lt;/strong&gt; method&amp;#160; shown in the above example) support lambda expressions.&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;FirstOrDefault&lt;/strong&gt; extension method of the Enumerable class returns the first item in the list or the default value for the object. If you pass it a lambda expression, it returns the first item in the list that matches the Boolean expression.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Lambda Expressions: An Introduction</title><link>http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-an-introduction.aspx</link><pubDate>Sun, 11 Oct 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1731659</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;My presentation at our local Code Camp was fun. I covered a lot of material and had a GREAT audience. Several people have asked for the slide information in my blog. So this is the start of several posts on lambda expressions.&lt;/p&gt;  &lt;p&gt;In addition, I wrote an article on lambda expressions that should appear in the Jan/Feb 2010 issue of Code Magazine. I’ll post the link as soon as its published.&lt;/p&gt;  &lt;p&gt;Lambda expressions are &lt;strong&gt;unnamed, inline functions&lt;/strong&gt;. They are new in Visual Basic 9 and C# 3.0 (VS 2008). You can use a lambda expression anywhere a delegate is required. For more information on delegates, see &lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/delegates.aspx"&gt;this post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Here are an additional set of posts that cover the slides and information from my talk:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/03/generics-building-a-list-of-customers.aspx"&gt;Generics: Building a List of Customers&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-finding-an-item-in-a-generic-list.aspx"&gt;Lambda Expressions: Finding an Item in a Generic List&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-syntax.aspx"&gt;Lambda Expressions: Syntax&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-predicate-delegates.aspx"&gt;Lambda Expressions: Predicate Delegates&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-action-delegates.aspx"&gt;Lambda Expressions: Action Delegates&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-func-delegates.aspx"&gt;Lambda Expressions: Func Delegates&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/10/16/lambda-expressions-execution.aspx"&gt;Lambda Expressions: Execution&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/03/lambdas-aggregating-strings.aspx"&gt;Lambdas: Aggregating Strings&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/07/02/lambda-expressions-finding-differences-in-two-lists.aspx"&gt;Lambda Expressions: Finding Differences in Two Lists&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Enumerable.Repeat</title><link>http://msmvps.com/blogs/deborahk/archive/2009/09/04/enumerable-repeat.aspx</link><pubDate>Fri, 04 Sep 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1720776</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;The &lt;strong&gt;Enumerable&lt;/strong&gt; class is new in .NET 3.5 and is part of the System.Linq namespace. It provides a set of static methods that allow you to query any object that implements IEnumerable, basically meaning any object that supports a for/each.&lt;/p&gt;  &lt;p&gt;This post focuses on the &lt;strong&gt;Repeat&lt;/strong&gt; method of the Enumerable class and some of the helpful things that this class can do for you.&lt;/p&gt;  &lt;p&gt;NOTE: Be sure to set a reference to System.Core.&lt;/p&gt;  &lt;p&gt;First, at the top of your code file, add this code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;using System.Linq;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Imports System.Linq&lt;/p&gt;  &lt;p&gt;(Or for VB you can import a namespace using the project properties.)&lt;/p&gt;  &lt;p&gt;You can then use the Enumerable.Repeat as shown in the following examples.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Initialize an array to -1 for each of 10 elements     &lt;br /&gt;int[] all1 = Enumerable.Repeat(-1, 10).ToArray(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Initialize an array to &amp;quot;A&amp;quot; for each of 10 elements     &lt;br /&gt;string[] allA = Enumerable.Repeat(&amp;quot;A&amp;quot;, 10).ToArray(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Convert a single value to an array     &lt;br /&gt;int value = 42;      &lt;br /&gt;int[] valueArray = Enumerable.Repeat(value, 1).ToArray(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Generate 10 random numbers     &lt;br /&gt;Random rand = new Random();      &lt;br /&gt;int[] randomArray = Enumerable.Repeat(0, 10).Select(      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i =&amp;gt; rand.Next(0,10)).ToArray(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Initialize a list with 5 Customer objects     &lt;br /&gt;List&amp;lt;Customer&amp;gt; custList = Enumerable.Repeat(      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Customer(), 10).ToList();&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Initialize an array to -1 for each of 10 elements     &lt;br /&gt;Dim all1() As Integer = Enumerable.Repeat(-1, 10).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Initialize an array to &amp;quot;A&amp;quot; for each of 10 elements     &lt;br /&gt;Dim allA() As String = Enumerable.Repeat(&amp;quot;A&amp;quot;, 10).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Convert a single value to an array     &lt;br /&gt;Dim value As Integer = 42      &lt;br /&gt;Dim valueArray() As Integer = Enumerable.Repeat(value, 1).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Generate 10 random numbers     &lt;br /&gt;Dim rand As Random = New Random()      &lt;br /&gt;Dim randomArray() As Integer = Enumerable.Repeat(0, 10).Select( _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Function(i) rand.Next(0, 10)).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Initialize a list with 5 Customer objects     &lt;br /&gt;Dim custList As List(Of Customer) = Enumerable.Repeat( _      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; New Customer(), 10).ToList()&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The first example initializes an array of 10 integers to –1.&lt;/p&gt;  &lt;p&gt;The second example initializes an array of 10 strings to &amp;quot;A&amp;quot;.&lt;/p&gt;  &lt;p&gt;The third example takes a single value and creates an array from it. This is often necessary when a method requires an array instead of a single element.&lt;/p&gt;  &lt;p&gt;The random number example provides another way to generate a random numbers. In this case, it generates 10 random numbers between 0 and 9. Note that this does &lt;em&gt;not&lt;/em&gt; ensure uniqueness, so the number 2 for example could occur multiple times in the list.&lt;/p&gt;  &lt;p&gt;The last example initializes a list of business objects to an empty set of objects. You could use object initializer syntax in this example to initialize all of the objects to some set of values, such as setting the State = &amp;quot;CA&amp;quot;. This could be useful in setting up data for testing or prototyping.&lt;/p&gt;  &lt;p&gt;Use the Repeat method any time you want to repeat values in an array or list.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item><item><title>Enumerable.Range</title><link>http://msmvps.com/blogs/deborahk/archive/2009/09/04/enumerable-range.aspx</link><pubDate>Fri, 04 Sep 2009 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1720768</guid><dc:creator>DeborahK</dc:creator><description>&lt;p&gt;The &lt;strong&gt;Enumerable&lt;/strong&gt; class is new in .NET 3.5 and is part of the System.Linq namespace. It provides a set of static methods that allow you to query any object that implements IEnumerable, basically meaning any object that supports a for/each.&lt;/p&gt;  &lt;p&gt;This post focuses on the &lt;strong&gt;Range&lt;/strong&gt; method of the Enumerable class and some of the helpful things that this class can do for you.&lt;/p&gt;  &lt;p&gt;NOTE: Be sure to set a reference to System.Core.&lt;/p&gt;  &lt;p&gt;First, at the top of your code file, add this code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;using System.Linq;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;Imports System.Linq&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;(Or for VB you can import a namespace using the project properties.)&lt;/p&gt;  &lt;p&gt;You can then use the Enumerable.Range as shown in the following examples.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In C#:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Initialize an array of numbers 0 - 9      &lt;br /&gt;int[] numbers = Enumerable.Range(0, 10).ToArray();&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Initialize an array with any arithmetic sequence      &lt;br /&gt;// This one does 10, 20, 30 ... 100       &lt;br /&gt;int[] arithmetic = Enumerable.Range(0, 10).Select(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i =&amp;gt; 10 + (10 * i)).ToArray(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Initialize an array with any geometric sequence      &lt;br /&gt;// This one does 2, 6, 18, ...&amp;#160; 39366       &lt;br /&gt;int[] geometric = Enumerable.Range(0, 10).Select(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i =&amp;gt; 2 * (int)Math.Pow(3,i)).ToArray(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// This one does 10, 5, 2.5, ... 0.01953125      &lt;br /&gt;double[] geometric2 = Enumerable.Range(0, 10).Select(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i =&amp;gt; 10 * Math.Pow(.5, i)).ToArray(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// Initialize an array with letters      &lt;br /&gt;// This one does A, B, ... J       &lt;br /&gt;string[] letters = Enumerable.Range(0, 10).Select(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i =&amp;gt; ((char)(&amp;#39;A&amp;#39; + i)).ToString()).ToArray(); &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;// This one does z, y, ... q      &lt;br /&gt;string[] letters2 = Enumerable.Range(0, 10).Select(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; i =&amp;gt; ((char)(&amp;#39;z&amp;#39; - i)).ToString()).ToArray();&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In VB:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Initialize an array of numbers 0 - 9      &lt;br /&gt;Dim numbers() As Integer = Enumerable.Range(0, 10).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Initialize an array with any arithmetic sequence      &lt;br /&gt;&amp;#39; This one does 10, 20, 30 ... 100       &lt;br /&gt;Dim arithmetic() As Integer = Enumerable.Range(0, 10).Select( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Function(i) 10 + (10 * i)).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Initialize an array with any geometric sequence      &lt;br /&gt;&amp;#39; This one does 2, 6, 18, ...&amp;#160; 39366       &lt;br /&gt;Dim geometric() As Integer = Enumerable.Range(0, 10).Select( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Function(i) CType(2 * (3 ^ i), Integer)).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; This one does 10, 5, 2.5, ... 0.01953125      &lt;br /&gt;Dim geometric2() As Double = Enumerable.Range(0, 10).Select( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Function(i) 10 * (0.5 ^ i)).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; Initialize an array with letters      &lt;br /&gt;&amp;#39; This one does A, B, ... J       &lt;br /&gt;Dim letters() As String = Enumerable.Range(0, 10).Select( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Function(i) (Chr(Asc(&amp;quot;A&amp;quot;) + i)).ToString()).ToArray() &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#65402e" face="Consolas"&gt;&amp;#39; This one does z, y, ... q      &lt;br /&gt;Dim letters2() As String = Enumerable.Range(0, 10).Select( _       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Function(i) (Chr(Asc(&amp;quot;z&amp;quot;) - i)).ToString()).ToArray()&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Note: Even though all of the above examples use ToArray to build an array, you could instead build lists using ToList.&lt;/p&gt;  &lt;p&gt;The first example uses the basics of the Range to build an array of integers. You can use this syntax to build any range of integers.&lt;/p&gt;  &lt;p&gt;The other examples combine the Range method with a lambda expression to build other types of numeric arrays.&lt;/p&gt;  &lt;p&gt;The arithmetic sequence example demonstrates how to build an array where the difference between each term is a constant. In this example, the constant is 10.&lt;/p&gt;  &lt;p&gt;The geometric sequence examples demonstrates how to build any array where each term after the first is generated by multiplying the previous one by a fixed non-zero number (common ratio). In the first example, the common ratio is 3. In the second example, the common ratio is 1/2.&lt;/p&gt;  &lt;p&gt;The alphabetic sequence uses the fact that the Unicode characters for the letters in the alphabet are in numerical sequence. So adding 1 to &amp;#39;A&amp;#39; gives you &amp;#39;B&amp;#39; and so on.&lt;/p&gt;  &lt;p&gt;Use the Range method any time you want to build a list or array of values in a logical sequence.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;</description></item></channel></rss>