<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Anay Kamat's Weblog &#187; lambdas</title>
	<atom:link href="http://anaykamat.com/tag/lambdas/feed/" rel="self" type="application/rss+xml" />
	<link>http://anaykamat.com</link>
	<description>Technology, Programming, Career, Fun, Friends And Thoughts</description>
	<lastBuildDate>Wed, 25 Aug 2010 12:20:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Simple equivalent of &#8220;With&#8221; statement in C#</title>
		<link>http://anaykamat.com/2009/08/09/simple-equivalent-of-with-statement-in-c-sharp/</link>
		<comments>http://anaykamat.com/2009/08/09/simple-equivalent-of-with-statement-in-c-sharp/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 16:06:29 +0000</pubDate>
		<dc:creator>Anay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[lambdas]]></category>

		<guid isPermaLink="false">http://anaykamat.com/?p=84</guid>
		<description><![CDATA[Consider the following class in C# public class Person { private string name; private int age; public string Name{ get {return name;} set { name = value; } } public int Age{ get {return age;} set { age = value; } } } If I want to set the value of Name and Age property [...]]]></description>
			<content:encoded><![CDATA[<p>Consider the following class in C#</p>
<pre class="brush: csharp;">
	public class Person
	{
		private string name;
		private int age;

		public string Name{
			get {return name;}
			set { name = value; }
		}

		public int Age{
			get {return age;}
			set { age = value; }
		}
	}
</pre>
<p>If I want to set the value of Name and Age property on the instance of Person class, I&#8217;ll need to refer to that instance for every property I need to set in the code. For example:</p>
<pre class="brush: csharp;">
var person = new Person();
person.Name = &quot;Super Man&quot;;
person.Age = 30;
</pre>
<p>It would have been great if C# had an equivalent of VB&#8217;s &#8220;With..End&#8221; statement, where we could refer to the instance of Person class only once and then refer to properties only.</p>
<p>Today, I came across this post &#8220;<a href="http://blog.bittercoder.com/PermaLink,guid,d1831805-dbf7-4b74-a6fd-2e9ed437c3d9.aspx" target="_blank">mucking about with hashes&#8230;</a>&#8220;, which shows how C# lambdas could be used as hashes. Using this concept, I implemented a simple extension method that simulates the behavior of VB&#8217;s &#8220;With..End&#8221; statement to some extent.</p>
<p>Here is the code for extension method:</p>
<pre class="brush: csharp;">
	public static class MetaExtensions
	{
		public static void Set(this object obj,params Func&lt;string,object&gt;[] hash){
				foreach(Func&lt;string,object&gt; member in hash){
					var propertyName = member.Method.GetParameters()[0].Name;
					var propertyValue = member(string.Empty);
					obj.GetType()
						.GetProperty(propertyName)
							.SetValue(obj,propertyValue,null);
				};
		}
	}
</pre>
<p>Using this extension method, we can set the value of properties on instance of Person class as follows:</p>
<pre class="brush: csharp;">
var person = new Person();
person.Set(
	Name =&gt; &quot;Super Man&quot;,
	Age =&gt; 30
);
</pre>
<p>Isn&#8217;t that cool?</p>
]]></content:encoded>
			<wfw:commentRss>http://anaykamat.com/2009/08/09/simple-equivalent-of-with-statement-in-c-sharp/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
