<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ahmed El-Halwagy's Blog</title>
	<atom:link href="http://halwagy.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://halwagy.wordpress.com</link>
	<description>A Geek blogging for all fellow Geeks !</description>
	<lastBuildDate>Tue, 03 Nov 2009 12:02:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='halwagy.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ahmed El-Halwagy's Blog</title>
		<link>http://halwagy.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://halwagy.wordpress.com/osd.xml" title="Ahmed El-Halwagy&#039;s Blog" />
	<atom:link rel='hub' href='http://halwagy.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Moved!</title>
		<link>http://halwagy.wordpress.com/2009/11/03/moved/</link>
		<comments>http://halwagy.wordpress.com/2009/11/03/moved/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 12:02:49 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=114</guid>
		<description><![CDATA[Hello there, Sorry, but this blog has been moved to This Site. This site will remain as is with no updates &#8211;only the old articles will stay here. To get the new articles that I write, please visit my other blog So .. See you there pals!<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=114&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hello there, <br />
Sorry, but this blog has been moved to <a href="http://galilyou.blogspot.com/"><b>This Site</b></a>. <br />
This site will remain as is with no updates &#8211;only the old articles will stay here. To get the new articles that I write, please visit my other <a href="http://galilyou.blogspot.com"><b>blog</b></a><br />
So .. See you there pals! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=114&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2009/11/03/moved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
		<item>
		<title>C# 4.0 Now Consumes Indexed Properties</title>
		<link>http://halwagy.wordpress.com/2009/11/02/c-4-0-now-consumes-indexed-properties/</link>
		<comments>http://halwagy.wordpress.com/2009/11/02/c-4-0-now-consumes-indexed-properties/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 08:05:28 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# 4.0]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/2009/11/02/c-4-0-now-consumes-indexed-properties/</guid>
		<description><![CDATA[If you dealt with COM Interop before, then you probably know what Indexed Properties mean. If you don’t, hang on with me and you will know in the coming few lines. Consuming Indexed Properties is a new feature to C# 4.0 Beta2. This is used to improve syntax like the following: var excel = new [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=112&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you dealt with COM Interop before, then you probably know what Indexed Properties mean. If you don’t, hang on with me and you will know in the coming few lines. Consuming Indexed Properties is a new feature to C# 4.0 Beta2. This is used to improve syntax like the following: </p>
<p>var excel = new Microsoft.Office.Interop.Excel.ApplicationClass();</p>
<p>excel.get_Range(“A1”);</p>
<p>This syntax can now be improved to get rid of get_Range(“A1”) and use an indexer accessor instead, here’s how C# 4.0 Beta 2 can do for you to improve this: </p>
<p>var range = excel.Range[“A1”];</p>
<p>So now, every time you use COM Interop and have to call get_x() and set_x(), you can now replace this with the new indexer syntax. I have to tell you –Well, you might have guessed it- that this is just a syntatic sugar, the compiler will do emit calls to get_x() and set_x() ultimately. </p>
<p>I think this little syntax improvement is pretty neat, however, people shouldn’t ask the very expected question “Well, now we can consume indexed properties in c#, why can’t we create it? we wanna create indexed properties! Indexed properties is a legal right! blah blah .. “. If C# allowed us to create indexed properties then I think, this will add an ambiguity that isn’t worth anything here. I mean take a look at the following code and tell me what would it mean to you, if C# enables you to create indexed properties? </p>
<p>obj.SomeProperty[1]</p>
<p>Does it mean that SomeProperty is a type that implements an indexer, or SomeProperty is a property that requires an indexer? See the ambiguity? </p>
<p>&#160;</p>
<p>What’s your thoughts on this, dear reader? </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=112&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2009/11/02/c-4-0-now-consumes-indexed-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
		<item>
		<title>When the increment is happening?</title>
		<link>http://halwagy.wordpress.com/2009/08/20/when-the-increment-is-happening/</link>
		<comments>http://halwagy.wordpress.com/2009/08/20/when-the-increment-is-happening/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 13:10:48 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=107</guid>
		<description><![CDATA[In my last post when I linked to a very interesting article on Eric Lippert&#8217;s blog. Now I want to follow up on the subject of the post increment (++) operator. Personally I used to think the the post increment operator will increment the value of its operand at the very end of the current [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=107&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
In my last <a href="http://halwagy.wordpress.com/2009/08/11/operators-precednce-vs-the-order-of-evaluation/">post</a> when I linked<br />
to a very <a href="http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx">interesting article</a> on Eric Lippert&#8217;s blog.<br />
Now I want to follow up on the subject of the post increment (++) operator.
</p>
<p>
Personally I used to think the the post increment operator will increment the value of its operand at the very end of the current<br />
statement (i.e. just before the terminating &#8220;;&#8221;). So according to my understanding, this expression (x+(y++)+y) would evaluate to 0&#8211;<br />
of course of both x and y were initialized to 0.
</p>
<p>
If you tried this example in the C# compiler you would find that the result of<br />
the expression is 1! (yes not 0 :S). Obviously I was wrong! Here&#8217;s how I thought the sequence of evaluating this expression (z = x+(y++)+y;)<br />
would go: First, x is added to y and the result is stored somewhere (a for example)<br />
	Second, the result of (#First) will be added to y again (note: y here is still 0, the increment didn&#8217;t happen yet)<br />
		and the result of this evaluation is stored somewhere (b for example)<br />
	Third, the assignment will happen; assigning the result of (#second &#8220;b&#8221;) to whatever on the left side of the &#8220;=&#8221; operator.<br />
	Fourth: Now (and only now) increment y. y = 1 now.
</p>
<p>
If this is right, I would get 0 as a result of Console.WriteLine(z); but it is NOT. This flow of evaluation is not correct.<br />
My understanding of the<br />
post increment operator is not right! (I&#8217;m doomed, eh!?).
</p>
<p>
So I checked up the c# documentation, still couldn&#8217;t find any specific statement<br />
about when exactly the increment happens. After an hour on the internet, I turned to my favorite site, my <a href="http://www.hanselman.com/blog/TheDeveloperTheoryOfTheThirdPlace.aspx"><br />
third place</a> now, Stackoverflow.com.
</p>
<p>
I logged in and posted a <a href="http://stackoverflow.com/questions/1260227/int-arr0-int-value-arrarr0-value-1">question</a>.<br />
Go ahead click the link and check the accepted answer, if you&#8217;re lazy, here&#8217;s my summary of the answer.
</p>
<p>
First you have to know that the ++ operator has an operation to perform with a specified precedence and a SIDE EFFECT. In short, the ++<br />
operator has the higher precednce than (* + &#8211; /). See the <a href="http://msdn.microsoft.com/en-us/library/aa691323(VS.71).aspx">specs</a><br />
This means that the operator will execute instantly! Yes Instantly! So the increment will be the first thing to happen (even if it&#8217;s<br />
post increment or pre-increment). The difference between pre and post comes from the side effect. When post incrementing the old value is<br />
stored, then the increment happens (i.e. the value of the variable increments),<br />
and the value that&#8217;s used to continue evaluating the expression is the (yeah, you guessed it) old value.
</p>
<p>
So to get back to the example (z = z +(y++) +y;), the order of evaluating this expression would be as follows: </p>
<ol>
<li> Store the value of y somwhere -say a. a now = 0. </li>
<li>Increment y (remember the increment has the highest precednece). </li>
<li> Add x to the value of a(which is the old value of y; that&#8217;s the side effect part of the operator) and save the result of<br />
the addition somewhere (say b). now b = 0; </li>
<li> Add the value of b to the value of y (what&#8217;s the value of y now? yes you&#8217;re right, it&#8217;s 1), and store the result somewhere<br />
say c. say now = 1. </li>
<li> Assign the value of c to z. z now = 1. </li>
</ol>
<p>
As you can see, the increment happens instantly. And its side effect forces the use of the old value of the incremented variable<br />
for evaluating the very next part of the expression. After that when trying to dereference the incremented value and get its value,<br />
you will actually get the incremented value.
</p>
<p><b><br />
Makes perfect sense to me now. What about you dear reader?<br />
<b></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/107/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=107&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2009/08/20/when-the-increment-is-happening/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
		<item>
		<title>Operator&#8217;s Precednce vs The Order of Evaluation</title>
		<link>http://halwagy.wordpress.com/2009/08/11/operators-precednce-vs-the-order-of-evaluation/</link>
		<comments>http://halwagy.wordpress.com/2009/08/11/operators-precednce-vs-the-order-of-evaluation/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 12:22:17 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#-mysteries]]></category>
		<category><![CDATA[OperatorPrecedence]]></category>
		<category><![CDATA[OrderOfEvaluation]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=99</guid>
		<description><![CDATA[A little ago Eric Lippert took a stand to the programming myths that people have about c#. Starting by his awesome The stack is an implementation detail then Not everything derives from object and today&#8217;s Precedence vs Order, Redux. I have to say it was an awesome job, very nice articles -just like everything Eric [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=99&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A little ago <a href="http://blogs.msdn.com/ericlippert/">Eric Lippert</a> took a stand to the programming myths that people have about c#. Starting by his awesome <a href="http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx">The stack is an implementation detail</a><br />
then <a href="http://blogs.msdn.com/ericlippert/archive/2009/08/06/not-everything-derives-from-object.aspx">Not everything derives from object</a> and today&#8217;s <a href="http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx">Precedence vs Order, Redux</a>. I have to say it was an awesome job, very nice articles -just like everything Eric writes. I strongly recommend that you scan through these articles, a lot of mysteries to be unveiled there. I&#8217;m not going to repeat what Eric said because I&#8217;m too proud to do! Well, not really! It&#8217;s just I&#8217;m not as good as Eric to think of providing  new versions of his articles (what new would these new versions have anyway?) I just want to stop by the last article <a href="http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx">Precedence vs Order, Redux</a>. I had a nice 12 parts conversation on facebook with one of my friends. At first I posted a <a href="http://twitter.com/Galilyou/status/3240774030">Twitter post</a> with a question and a link to Eric&#8217;s article. I got a comment from <a href="http://www.facebook.com/profile.php?id=775684972">one of my friends</a> &#8211;who stands to be one of the very best students at Century 21, IBM. The man&#8217;s comment was &#8220;value = 1&#8243;. Nice and easy! Well, that wasn&#8217;t what I expected from this friend -I know the answer, I run the sample code, and seen the value of value <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , and I read Eric&#8217;s article. What I expected him to say is 0. Because as far as I know, this program should work this way:</p>
<p>1- Get the value of arr[0] .. the inner expression arr[0] .. 0 returned</p>
<p>2- Get the value of arr[0] again .. the outer arr[arr[0]] .. still 0 returned</p>
<p>3- Now assign the value of 0 to the variable value.</p>
<p>4- Now and not before now, Increment arr[0].</p>
<p>Apparently what I know is wrong. The increment didn&#8217;t happen the moment I expected it to happen &#8211;after assigning the value. Actually it happened before assigning the value. Frankly I don&#8217;t understand why!? I tried to search the C# spec and couldn&#8217;t find anything to state strictly when the increment should happen in case of post increment and pre-increment.<br />
Another interesting and confusing case is a snippet I saw in a comment by Peter Ibboston on the same article by Eric:</p>
<blockquote><p>
The one that confused me is this C# code which gives different results in C &amp; C#</p>
<p>int[] data={11,22,33};int i=1;data[i++]=data[i]+5;</p>
<p>I couldn&#8217;t find a bit in the C# spec that said anything about when increment should happen. I had presumed that the rvalue would be evaluated first followed by the lvalue.</p>
<p>Any ideas where I missed it.
</p></blockquote>
<p>To finalize I would say, there&#8217;s no documentation to specifically tell when the increment will happen &#8211;AFAIK of course. Eric emphasized an interesting statement in his article too, he said:</p>
<blockquote><p>I emphasize that the logical sequence is well-defined because the compiler, jitter and processor are all allowed to change the actual order of events for optimization purposes, subject to the restriction that the optimized result must be indistinguishable from the required result in single-threaded scenarios.</p></blockquote>
<p>The compiler or the Jitter or the processor are all able to modify the logical sequence for optimization purposes. The optimization shouldn&#8217;t affect the output in a single-threaded application, however, in a multi-threaded app, it might be observable. Yeah I see. I still don&#8217;t know why is this working this way? When exactly the increment is happening?</p>
<p>What do you think dear reader?</p>
<p>Please help me clarify the facts, and eliminate these confusions.</p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/99/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/99/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/99/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=99&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2009/08/11/operators-precednce-vs-the-order-of-evaluation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
		<item>
		<title>Different prespecitves on the title &#8220;Google announced Google Chrome OS!&#8221;</title>
		<link>http://halwagy.wordpress.com/2009/07/09/differenct-prespecitves-on-the-title-google-announced-google-chrome-os/</link>
		<comments>http://halwagy.wordpress.com/2009/07/09/differenct-prespecitves-on-the-title-google-announced-google-chrome-os/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 12:25:51 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[GoogleChromeOS]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=84</guid>
		<description><![CDATA[Yeah, I&#8217;m back. I know it&#8217;s been a long time since I made my last blog post. Actually there&#8217;s no reason why I stopped blogging -besides being lazy of course. However, I&#8217;m back, and I&#8217;m back with big news. As you&#8217;ve read, the title of this post, is Google announced Chrome OS! -see the exclamation [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=84&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Yeah, I&#8217;m back.<br /> I know it&#8217;s been a long time since I made my last blog post. Actually there&#8217;s no reason why I stopped blogging -besides being lazy of course. However, I&#8217;m back, and I&#8217;m back with big news. As you&#8217;ve read, the title of this post, is <a href="http://googleblog.blogspot.com/2009/07/introducing-google-chrome-os.html">Google announced <strong>Chrome OS!</strong></a> -see the exclamation mark? I&#8217;m not going to claim -like others- that I was not shocked, actually I was. Why yet another operating system, ain&#8217;t Mac, Linux, and Windows enough already? Actually there have been a lot of explanations of this call of Goolge, since yesterday. <a href="http://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a> wrote an interesting article about his vision of Chrome OS. Actually Kent&#8217;s article is very interesting -I&#8217;m sure you can judge that by reading only the title &#8220;Chrome OS is Worse, That’s the Point&#8221;. Kent, started his discussion with Chrome -the browser, it&#8217;s important to be specific from now on:). He mentioned that Chrome&#8217;s advantages overweights the disadvantages so that he uses it. </p>
<p><em><br />
 So I decided to try it, to simulate using Chrome as my operating system. I made it my default browser (in spite of Microsoft’s periodic attempts to change my preference) and expanded it to full screen. From then on I did everything I could on the web. </em></p>
<p>The key idea here -according to Kent- is that, It doesn&#8217;t matter if the new invention is better or worse, what matters is the alternatives that this new invention provides for people to do things they care for (I think email, and all web-stuff in general is something most people care for nowadays.</p>
<p>
<em><br />
Innovations that start out worse need to be better at something new that matters. Imagine never having to install an application again. Never having to back up. Never having to reinstall the OS because it’s just gotten way too weird. I’d give up a lot to gain that. That was the point of telling you about my experiment: I’ve seen the future and it’s not so bad.<br />
</em>
</p>
<p>
And here&#8217;s what Kent thinks MS, Apple, and Linux should do to stay strong on the desktop world: </br><br />
<em>To remain strong in desktop operating systems, though, Apple or Microsoft or the Linux desktops would have to abandon their current profit model, find a fresh ultra-simplification twist, and run the new business far from rational-but-doomed headquarters (Merlin, Oregon has a lovely abandoned sawmill site ready for development, in case you’re interested). They aren’t likely to do so, though, because it makes no sense</em>
</p>
<p>
The question about the potential success or failure of Google Chrome OS will remain alive till a long time: -as Google Chrome OS will be shipped only later next year. I&#8217;m not sure! What do you think dear reader?</p>
<p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=84&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2009/07/09/differenct-prespecitves-on-the-title-google-announced-google-chrome-os/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
		<item>
		<title>Anonymous Methods and Lambdas.</title>
		<link>http://halwagy.wordpress.com/2009/01/10/anonymous-methods-and-lambdas/</link>
		<comments>http://halwagy.wordpress.com/2009/01/10/anonymous-methods-and-lambdas/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 02:16:29 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=74</guid>
		<description><![CDATA[A while ago, when I posted my last post on this blog which was titles (Delegates and Events &#8211; A Look Back), I stated that it was an introduction to Lambda Expressions. Though it took me more than a month to re-blog again, here i&#8217;m continuing the journey on the C# 3.0 language enhancements features. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=74&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A while ago, when I posted my last <a href="http://halwagy.wordpress.com/2008/12/11/delegates-and-events-%E2%80%93-a-look-back/">post</a> on this blog which was titles (Delegates and Events &#8211; A Look Back), I stated that it was an introduction to Lambda Expressions. Though it took me more than a month to re-blog again, here i&#8217;m continuing the journey on the C# 3.0 language enhancements features. The first thing we will discuss here is anonymous methods, as you know anonymous methods is just a shorthand way for subscribing to events and providing handlers all in one shot. The following code example shows the old way to subscribe to a Click event on a normal System.Windows.Forms.Button object.</p>
<p><a href="http://halwagy.files.wordpress.com/2009/01/code1.jpg"><img class="alignleft size-full wp-image-75" title="code1" src="http://halwagy.files.wordpress.com/2009/01/code1.jpg?w=480&#038;h=162" alt="code1" width="480" height="162" /></a></p>
<p>This is the very classic way of subscribing to event. You simply need to provide a pre-defined delegate (in this case EventHandler) that points to a function matching a specific signature (in this case returns void and accepts two input parameters an System.Object parameter, and System.EventArgs parameter) . Using anonymous methods, life can be easier. Check out the next code sample.</p>
<p><a href="http://halwagy.files.wordpress.com/2009/01/code2.jpg"><img class="alignleft size-full wp-image-76" title="code2" src="http://halwagy.files.wordpress.com/2009/01/code2.jpg?w=480" alt="code2" /></a></p>
<p>As you can see here, all I needed to do in order to subscribe to the Click event on the Button object is to simply write my code that handles the event without having to define a whole new function just to handle this event, and also without having to know the event&#8217;s delegate signature.</p>
<p><strong>Lambdas </strong></p>
<p>Lambda expressions are another new way in C# 3.0 to substitute delegates in certain places. Now consider the following example, if you have a List&lt;int&gt;  and you want to filter this list and get only all the odd numbers out of it. One solution that might come handy is to use the FindAll method of your generic List&lt;int&gt;. FindAll expects one argument which in fact is of type System.Predicate&lt;T&gt;. System.Predicate&lt;T&gt; is a delegate that can point to any methods returns bool and takes a single argument T. The point is when FindAll was designed it was designed as this(take each item, check it, and then tell me if it should be included in the result set of the call).</p>
<p>The follwoing example illustrates the use of the smart FindAll method, take a look:</p>
<p><a href="http://halwagy.files.wordpress.com/2009/01/code3.jpg"><img class="alignleft size-full wp-image-77" title="code3" src="http://halwagy.files.wordpress.com/2009/01/code3.jpg?w=480" alt="code3" /></a></p>
<p>As you see here I&#8217;ve defined a method called IsOdd that takes a single integer parameter and returns a boolean value indicating whether the passed in parameter is odd or not. I then called the FindAll method on my list of integers (numbers) passing in a new Predicated delegate that points to IsOdd. What is going to happen here is my numbers list will take each element of it and pass it to the IsOdd(the method that the Predicate delegate points to) and check the value returned, if true, then the item will be added to the result set. If false then the item will be ignored. If you run the above code you will get the following result.</p>
<p><a href="http://halwagy.files.wordpress.com/2009/01/code41.jpg"><img class="alignleft size-full wp-image-79" title="code41" src="http://halwagy.files.wordpress.com/2009/01/code41.jpg?w=480" alt="code41" /></a></p>
<p>Now, what if you don&#8217;t want to define this whole IsOdd method that makes a really tiny job here and will not be reused by any other peace of code? Well, you guessed it, use anonymous method syntax like this:<br />
<a href="http://halwagy.files.wordpress.com/2009/01/code5.jpg"><img src="http://halwagy.files.wordpress.com/2009/01/code5.jpg?w=480&#038;h=44" alt="code5" title="code5" width="480" height="44" class="alignleft size-full wp-image-80" /></a></p>
<p>A lambda expression is another handy way for providing an anonymous method. The syntax might seem clumsy at first, but once you get it, you never quit it <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
<p><a href="http://halwagy.files.wordpress.com/2009/01/code6.jpg"><img src="http://halwagy.files.wordpress.com/2009/01/code6.jpg?w=480" alt="code6" title="code6" class="alignleft size-full wp-image-81" /></a></p>
<p>What you see here is a lambda expression in action. The FindAll method expects a delegate and this time instead of passing it a delegate or an anonymous method I passed in well, a lambda, a lambda that will operate on one single integer parameter and return the the result of the expression x % 2 != 0.<br />
The thing that most people find uncomfortable about lambdas is that, they can&#8217;t pronounce it, yeah, they write it but they can&#8217;t pronounce it. Our above example will be pronounce as follows (My only parameter will be processed this way &#8220;as what between the {} stats&#8221;).<br />
A lambda expression can be in one of two forms; a single line form and a code block form. In our example we wrote a single statement in between two curly brackets, in fact I could have wrote any number of statements as needed. I will show you how lambda expressions can be appeared in a single line form:<br />
<a href="http://halwagy.files.wordpress.com/2009/01/code7.jpg"><img src="http://halwagy.files.wordpress.com/2009/01/code7.jpg?w=480" alt="code7" title="code7" class="alignleft size-full wp-image-82" /></a></p>
<p>Well that&#8217;s it for lambdas, any questions or suggestions feel absolutely free to leave a comment.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/74/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=74&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2009/01/10/anonymous-methods-and-lambdas/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>

		<media:content url="http://halwagy.files.wordpress.com/2009/01/code1.jpg" medium="image">
			<media:title type="html">code1</media:title>
		</media:content>

		<media:content url="http://halwagy.files.wordpress.com/2009/01/code2.jpg" medium="image">
			<media:title type="html">code2</media:title>
		</media:content>

		<media:content url="http://halwagy.files.wordpress.com/2009/01/code3.jpg" medium="image">
			<media:title type="html">code3</media:title>
		</media:content>

		<media:content url="http://halwagy.files.wordpress.com/2009/01/code41.jpg" medium="image">
			<media:title type="html">code41</media:title>
		</media:content>

		<media:content url="http://halwagy.files.wordpress.com/2009/01/code5.jpg" medium="image">
			<media:title type="html">code5</media:title>
		</media:content>

		<media:content url="http://halwagy.files.wordpress.com/2009/01/code6.jpg" medium="image">
			<media:title type="html">code6</media:title>
		</media:content>

		<media:content url="http://halwagy.files.wordpress.com/2009/01/code7.jpg" medium="image">
			<media:title type="html">code7</media:title>
		</media:content>
	</item>
		<item>
		<title>Delegates and  Events – A Look Back.</title>
		<link>http://halwagy.wordpress.com/2008/12/11/delegates-and-events-%e2%80%93-a-look-back/</link>
		<comments>http://halwagy.wordpress.com/2008/12/11/delegates-and-events-%e2%80%93-a-look-back/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 12:23:38 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[C# 3.0]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=65</guid>
		<description><![CDATA[Every .NET programmer has used delegates in a way or another and hopefully appreciated the flexibility delegates provide. A delegate is simply a type-safe, object oriented function pointer. A pointer that can points to a function and can be used to dynamically invoke methods. Take a look at the following example on how to use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=65&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:14pt;line-height:115%;">Every .NET programmer has used delegates in a way or another and hopefully appreciated the flexibility delegates provide. A delegate is simply a type-safe, object oriented function pointer. A pointer that can points to a function and can be used to dynamically invoke methods. Take a look at the following example on how to use the old fashion function pointer.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">#include</span><span style="font-size:14pt;font-family:&quot;"> <span style="color:#a31515;">&lt;iostream&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">using</span><span style="font-size:14pt;font-family:&quot;"> <span style="color:blue;">namespace</span> std;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:green;">//Function prototypes.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">int</span><span style="font-size:14pt;font-family:&quot;"> Add( <span style="color:blue;">int</span>, <span style="color:blue;">int</span>) ;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">int</span><span style="font-size:14pt;font-family:&quot;"> Subtract ( <span style="color:blue;">int</span>, <span style="color:blue;">int</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">void</span><span style="font-size:14pt;font-family:&quot;"> main( <span style="color:blue;">void</span> ) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">int</span> firstNumber = 0;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">int</span> secondNumber = 0;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">int</span> result = 0;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">char</span> answer = <span style="color:#a31515;">&#8216;a&#8217;</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:green;">//The declaration of function pointer.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">int</span> (*funcPtr) (<span style="color:blue;">int</span>, <span style="color:blue;">int</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">cout &lt;&lt; <span style="color:#a31515;">&#8220;Plz enter the first number !&#8221;</span> &lt;&lt; endl;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">cin &gt;&gt; firstNumber;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">cout &lt;&lt; <span style="color:#a31515;">&#8220;Plz enter the second number !&#8221;</span>&lt;&lt; endl;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">cin &gt;&gt; secondNumber;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">cout &lt;&lt; <span style="color:#a31515;">&#8220;Add or Subtract (A/S) ?&#8221;</span> &lt;&lt; endl;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">cin &gt;&gt; answer;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">if</span>(answer == <span style="color:#a31515;">&#8216;a&#8217;</span>) </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">funcPtr = Add; <span style="color:green;">// Assigning a function to the pointer.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">else</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">funcPtr = Subtract;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:green;">//calling the function using the function pointer.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">result = funcPtr(firstNumber, secondNumber);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">cout &lt;&lt; <span style="color:#a31515;">&#8220;The result is &#8220;</span> &lt;&lt; result &lt;&lt; endl;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">getchar();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:green;">//actual functions implementation.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">int</span><span style="font-size:14pt;font-family:&quot;"> Add( <span style="color:blue;">int</span> x, <span style="color:blue;">int</span> y) { </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">return</span> x + y;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">int</span><span style="font-size:14pt;font-family:&quot;"> Subtract( <span style="color:blue;">int</span> x, <span style="color:blue;">int</span> y) { </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">return</span> x &#8211; y;</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">That’s a complete C++ program to demonstrate the use of a function pointer. As you can see I’ve declared a function pointer funcPtr that is ready to point to any function that returns integer and takes two integers as parameters.</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;color:blue;">int</span><span style="font-size:14pt;line-height:115%;font-family:&quot;"> (*funcPtr) (<span style="color:blue;">int</span>, <span style="color:blue;">int</span>);</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">After declaring the pointer and prompting the user for input, it’s now time to assign a value to this pointer. As this pointer is declared to point to function, so the value assigned to it is a function. The syntax used to assign a value to the function pointer is: </span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;"><br />
</span><span style="font-size:14pt;line-height:115%;">PointerName = FunctionName; (i.e. </span><span style="font-size:14pt;line-height:115%;font-family:&quot;">funcPtr = Add;)</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">Now we have everything set, the function pointer is declared and is pointing to a valid function. We can now call that function using that pointer name (remember pointers are variables that holds the memory address of other variables or functions on the program, and can be used to indirectly access those variables values or call the target functions.) as follows: </span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">result = funcPtr(firstNumber, secondNumber);</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">This line is used to indirectly invoke the method that funcPtr is pointing to and passing in two integer variables (firstNumber and secondNumber) and also holding the function’s return value in yet another integer variable called result. The benefit of using this technique (regardless of the clumsy syntax) is that you can determine function calls according to user input in a concise way. Delegates basically do more or less the same job but in a more cleaner way. If you took a look at the above example (specially at this line<br />
<span style="color:blue;">int</span> (*funcPtr) (<span style="color:blue;">int</span>, <span style="color:blue;">int</span>);) you would notice that I have declared a function pointer funcPtr that can point to any function that returns integer and accepts two intger parameters. The dangerous thing about that line is that, I have not initialized that pointer which makes it poiting to a random place in memory and if I called it right a way without assigning a value to it, it would most probably cause my program to crash. (PS: you can use the following syntax to initialize that pointer to NULL at first declaration<br />
<span style="color:blue;">int</span> (*funcPtr) (<span style="color:blue;">int</span>, <span style="color:blue;">int</span>) = NULL;).</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">If you tried to use that pointer (funcPtr) before assigning value to it, the compiler will simply let you go, it will not generate an error to warn you from the potential risk. This is the real bad thing about function pointers and pointers in general, they’re flexible and powerful, but they are NOT safe.</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">A delegate is a safe, object-oriented replacement to the old function pointer. You can use delegates to indirectly invoke methods both in synchronous mode and asynchronous mode. As you may know delegates in C# are types, like classes, interfaces, structs and enums. They can be declared on a namespace level so you can reuse them in all the types in that namespace and mybe even across namspaces inside the boundary of one assembly or cross assemblies. To declare a delegate you use the following syntax. </span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;color:blue;">public</span><span style="font-size:14pt;line-height:115%;font-family:&quot;"> <span style="color:blue;">delegate</span> <span style="color:blue;">int</span> <span style="color:#2b91af;">BinaryDel</span>(<span style="color:blue;">int</span> x, <span style="color:blue;">int</span> y);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">Here we declared a delegate (modern function pointer) called BinaryDel that can point to any method that returns an integer and takes two integer parameters. To put this delegate in action take a look at the following example: </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">class</span> <span style="color:#2b91af;">Program</span> {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:green;">//Instantiating a new BinaryDel object</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">BinaryDel</span> myDel = <span style="color:blue;">new</span> <span style="color:#2b91af;">BinaryDel</span>(Add);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">int</span> result = myDel(5, 6);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;The result is {0}&#8221;</span>, result);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.ReadLine();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">int</span> Add(<span style="color:blue;">int</span> x, <span style="color:blue;">int</span> y) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">return</span> x + y;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">int</span> Subtract(<span style="color:blue;">int</span> x, <span style="color:blue;">int</span> y) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">return</span> x &#8211; y;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">As you can see here, the first thing I’ve done in order to use this BinaryDel is to instantiate a new instance of this delegate using the new keyword and passing in a method name in the constructor. This is the name of the method that myDel (the instance of BinaryDel) will be pointing to. For now delegates do not have a lot to offer more than function pointer except for the type-safety that is provided through the compile-time check. Delegates are more than that, a delegate can point to more than one method and invoke them all respectfully. Each delegate itself is a class that extends the System.MulticastDelegate class, this class has a collection called invocation list which is a collection of .. well .. delegates. You can use the (+=)operator on any instance of a delegate to add method addresses to that invocation list and later then you can call them all.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">class</span><span style="font-size:14pt;font-family:&quot;"> <span style="color:#2b91af;">Program</span> {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:green;">//Instantiating a new BinaryDel object</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">BinaryDel</span> myDel = <span style="color:blue;">new</span> <span style="color:#2b91af;">BinaryDel</span>(Add);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">myDel += <span style="color:blue;">new</span> <span style="color:#2b91af;">BinaryDel</span>(Subtract);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">myDel += Multiply;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">int</span> result = myDel(5, 6);<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.ReadLine();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">int</span> Add(<span style="color:blue;">int</span> x, <span style="color:blue;">int</span> y) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Inside Add method&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">return</span> x + y;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">int</span> Subtract(<span style="color:blue;">int</span> x, <span style="color:blue;">int</span> y) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Inside Subtract method&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">return</span> x &#8211; y;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">int</span> Multiply(<span style="color:blue;">int</span> x, <span style="color:blue;">int</span> y) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Inside Multiply method&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">return</span> x * y;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">That’s an update to the first example. In this example I’ve updated the Add and Subtract methods to print a line to the console indicating that each of them has been called. I also added a brand new method called multiply that also returns an integer and takes two integer parameters (i.e. matches the delegate signature.)I then used the += operator to add both Subtract and Multiply methods to the invocation list of myDel. Now myDel points to three methods Add which is passed to it in the constructor, Subtract, and Multiply that are added to it using the += operator. </span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;"><br />
PS: when using the += operator on a delegate instance, it expects another instance of the same delegate to be added to the invocation list of the left side delegate of the assignment. So at the first time I used the syntax (myDel += <span style="color:blue;">new</span> <span style="color:#2b91af;">BinaryDel</span>(Subtract);) to add new instance of BinaryDel pointing to Subtract to the invocation list of my current instance myDel, this is the expected behavior because as you may recall I mentioned that each delegate’s invocation list is simply a list of the same delegate items, each of which points to one or more methods. However, the syntax I used next is different than that<br />
(myDel += Multiply;). This is the same as the previous syntax. The compiler here will create a new instance of BinaryDel pointing to Multiply and then will add that instance to the invocation list of myDel. It’s also worth mentioning that you can remove method addresses from any delegates invocation list using the -= operator. Consult the .NET framework documentation for more details.</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">When calling these methods using myDel like so myDel(5, 6); they will be called in order meaning that the Add method will be called first then the Subtract then the Multiply (you may know guess that the invocation list of myDel is implemented as a Queue .. smart cookie </span><span style="font-size:14pt;line-height:115%;font-family:Wingdings;"><span>J</span></span><span style="font-size:14pt;line-height:115%;font-family:&quot;"> ). Now the question is “What is the value of result?” the answer is “The value of result is the return value of the last method in the invocation list, in our case multiply.</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">One more feature of delegates is that they provide the ability to invoke methods asynchronously throught the delegates bulit in method BeginInvoke. BeginInvoke will invoke the methods on a new background thread, and the main thread will keep executing at the same time, then when you are ready to receive the return of that method you called by the delegate you can call EndInvoke on the very same delegate. You can also provide an AsyncCallBack delegate that point to some method –Say A- as a parameter to begin invoke, this method –A- will be called automatically after the execution on the background thread ends. or further details consult the .NET framework decumentation.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">Delegates can also be used as function parameters and function return types. However, the primary usage for delegates in the .NET universe is to subscribe to events. An event is a flag raised by an object to indicate the various phases of the object life cycle. Assume we have a class called Employee that represents an actual employee. We will use events to flag some key time-points in the employee life cycle, points like when the employee gets hired or when picked for an external mission or when he or she gets fired. The code for the employee class my more or less look like this:</span></p>
<p><span style="color:green;">//Custom delegate to use for the employee events.</span><br />
<span style="color:blue;">public</span> <span style="color:blue;">delegate</span> <span style="color:blue;">void</span> <span style="color:#2b91af;">EmployeeDel</span>(<span style="color:#2b91af;">Employee</span> e, <span style="color:blue;">string</span> action);<span> </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;color:blue;">public</span><span style="font-size:14pt;font-family:&quot;"> <span style="color:blue;">class</span> <span style="color:#2b91af;">Employee</span> {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:green;">//Creating the events.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">event</span> <span style="color:#2b91af;">EmployeeDel</span> OnGettingARaise = <span style="color:blue;">null</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">event</span> <span style="color:#2b91af;">EmployeeDel</span> OnPromoting = <span style="color:blue;">null</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">int</span> Age { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">float</span> Salary { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">string</span> Name { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">string</span> Position { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">void</span> ShowDirectMangerSomeLove() {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;EMployee&gt;&gt; Mr Manager .. you&#8217;re the best <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .. !&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Manger&gt;&gt; thanks {0} you got a raise !&#8221;</span>, <span style="color:blue;">this</span>.Name);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:green;">//Increasing the salary by 5</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">this</span>.Salary += 5;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:green;">//Raising the event if there are any subscribers.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">if</span> (OnGettingARaise != <span style="color:blue;">null</span>)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">OnGettingARaise(<span style="color:blue;">this</span>, <span style="color:#a31515;">&#8220;Having a raise&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">void</span> ShowCEOSomeLove() {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;&gt;&gt;Employee: Mr CEO .. You&#8217;re leading us right to the top&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;&gt;&gt;CEO: thanks {0} .. you know what .. I really think you should be in a higher position&#8221;</span>, <span style="color:blue;">this</span>.Name);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">this</span>.Position = <span style="color:#a31515;">&#8220;Manager&#8221;</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">this</span>.Salary += 200;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:green;">//raising the event if there&#8217;s any subscribers.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">if</span> (<span style="color:blue;">this</span>.OnPromoting != <span style="color:blue;">null</span>)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">OnPromoting(<span style="color:blue;">this</span>, <span style="color:#a31515;">&#8220;Got promoted !&#8221;</span>);<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">Here I have declared a custom delegate called EmployeeDel that can point to any method that returns void and takes an Employee and a string as parameters. Then inside the Employee class declaration I created two events OnGettingARaise and OnPromoting, both using the EmployeeDel delegate. In the ShowDirectManagerSomeLove() method I raise the OnGettingARaise event passing in the current object and a string indicating that the employee has a raise, I then increase the employee’s salary as a result of showing his manager some love </span><span style="font-size:14pt;font-family:Wingdings;"><span>J</span></span><span style="font-size:14pt;font-family:&quot;">. As you notice here because the OnGettingARaise event is declared of type EmployeeDel, it has to pass in the required parameters for this EmployeeDel (Employee, string) when it’s being raised. To see this Employee class in action, take a look at the following:<br />
<span style="color:blue;"><br />
class</span> <span style="color:#2b91af;">Program</span> {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Employee</span> shankl = <span style="color:blue;">new</span> <span style="color:#2b91af;">Employee</span>() {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">Name = <span style="color:#a31515;">&#8220;Shankl Shankool&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">Age = 30,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">Salary = 2000f,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">Position = <span style="color:#a31515;">&#8220;Sales Man&#8221;</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">};</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">shankl.OnGettingARaise += <span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeDel</span>(HandleEmployeeGettingARaise);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">shankl.ShowDirectMangerSomeLove();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">void</span> HandleEmployeeGettingARaise(<span style="color:#2b91af;">Employee</span> e,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:blue;">string</span> action) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;"><span style="color:#a31515;">&#8220;{0} who works as {1} has got a raise .. \nhis salary now is {2}&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">e.Name, e.Position, e.Salary);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:14pt;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:14pt;line-height:115%;font-family:&quot;">The program class containing the Main method defines a method called HandleEmployeeGettingARaise that matches the EmployeeDel delegate singuature as it returns void and takes two input parameters (Employee, string). This method can be used to handle the OnGettingARaise and OnPromoting events for the employee class as it matches the type of these events (EmployeeDel). In Main I’ve declared a new Employee with the name “Shankl Shakol” and 30 years of Age and 2000$ salary and works as a “Sales Man” using the object initializaiton syntax. The next line is the line where I subscribe to the OnGettingARaise event of the Employee object-shankl-and provide delegate of the type EmployeeDel that points to my HandleEmployeeGettingARaise method. This means that when this event is raised my handler (HandleEmployeeGettingARaise mehtod) will be executed. Run the code and examine it yourself.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/65/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=65&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2008/12/11/delegates-and-events-%e2%80%93-a-look-back/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
		<item>
		<title>Auto Implemented Properties, and Object Initialization Syntax</title>
		<link>http://halwagy.wordpress.com/2008/11/25/auto-implemented-properties-and-object-initialization-syntax/</link>
		<comments>http://halwagy.wordpress.com/2008/11/25/auto-implemented-properties-and-object-initialization-syntax/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 17:52:16 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[C# 3.0]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=49</guid>
		<description><![CDATA[As a person who develops applications for business, there&#8217;s no doubt that most of the times you have to create classes that hold nothing but data –Which you most of the times would get them generated some way or another). Now imagine that you have a class called EmployeeData which is designed to hold only [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=49&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As a person who develops applications for business, there&#8217;s no doubt that most of the times you have to create classes that hold nothing but data –Which you most of the times would get them generated some way or another). Now imagine that you have a class called EmployeeData which is designed to hold only data of employees. This class can more or less look like the following.</p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">class</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:#2b91af;">EmployeeData</span> {<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//Member Variables.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">private</span> <span style="color:#2b91af;">Guid</span> id;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">private</span> <span style="color:blue;">string</span> name;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">private</span> <span style="color:blue;">int</span> age;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">private</span> <span style="color:blue;">string</span> address;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">private</span> <span style="color:blue;">float</span> salary;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">private</span> <span style="color:blue;">string</span> position;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//Public access properties.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:#2b91af;">Guid</span> ID {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">get</span> { <span style="color:blue;">return</span> <span style="color:blue;">this</span>.id; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">set</span> { <span style="color:blue;">this</span>.id = <span style="color:blue;">value</span>; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}<span> </span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal">As you can see here, you define the class variables as private and then encapsulate them in properties to enforce some business logic on the access of these variables. However, most of the times your properties don’t have any business logic code or validation in either of their Getter or Setter accessors. Though the properties are totally useless in this case, you still have to implement them just to have the OO look. Auto Implemented Properties is a capability added to C#3.0 and higher to save you some keystrokes (actually a lot of key strokes). If you have to implement a property to provide access to a variable in your class without having any code to run while getting or setting a value to this variable, use auto implemented property as follows.</p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">public</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:#2b91af;">Guid</span> ID { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">string</span> Name { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">int</span> Age { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">In the following code, I&#8217;ve declared three public properties ID, Name, and Age. I took advantage of the auto implemented properties syntax and saved my self some keystrokes by simply not adding a get and set bloc(though the accessors themselves already exist). The magic part here is the &#8220;;&#8221; semicolon after the get and the set accessor; this semicolon indicates that the compiler will automatically generate a private field with the type of the property (System.GUID, or System.String, or System.Int32 in our case) and will have the get and set retrieves and sets a value to that automatically generated variable. It worth mentioning that you can&#8217;t reference this variable directly, you have to use the property to access it. Also the name of this automatically generated variable is a really compiler-kina names (e.g. &#8220;x3ydfa34343adfa343&#8243;). The auto implemented properties syntax does not add any magic to your code, it simply saves you some keystrokes by instructing the compiler to generate a variable to you and allows access to this variable through your properties.</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">2- Object Initialization Syntax.</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">As an OO programmer, creating objects is a huge part of your daily job duties. You most of the times instantiate new objects of your classes or the base library class to work with and mainpulate in order to get your task done. C# 3.0 added a new capability for programmers to easily create instances of objects with different state. For example if you are creating an employee object named John Doe with 25 years of age and works as a Sales Representitive; your code will look like this :</span></p>
<p class="MsoNormal" style="text-indent:36pt;line-height:normal;margin:0 0 .0001pt 36pt;"><span style="font-size:10pt;font-family:&quot;color:#2b91af;">EmployeeData</span><span style="font-size:10pt;font-family:&quot;"> e = <span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeData</span>();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">e.Name = <span style="color:#a31515;">&#8220;John Doe&#8221;</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">e.Age = 25;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">e.Position = <span style="color:#a31515;">&#8220;Sales Representative&#8221;</span>;</span></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">As you can see, it took you 4 lines to create one employee instance. In order to avoid the hassle and save ourselves some time when creating employees we may create some overloaded constructors in the EmployeeData class and instantiate objects using these custom constructors like so: </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 36pt;"><span style="font-size:10pt;font-family:&quot;color:blue;">public</span><span style="font-size:10pt;font-family:&quot;"> EmployeeData(<span style="color:blue;">string</span> name, <span style="color:blue;">int</span> age, <span style="color:blue;">string</span> position) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">this</span>.Name = name;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">this</span>.Age = age;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">this</span>.Position = position;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;"><span style="color:green;">//Instantiating an object using the custom constructor</span><br />
<span style="color:#2b91af;">EmployeeData</span> x = <span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeData</span>(<span style="color:#a31515;">&#8220;John Doe&#8221;</span>, 25, <span style="color:#a31515;">&#8220;Sales Representative&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">Obviously this saves a lot of keystrokes, precisely 3 lines for each employee. As you can see her the way to get to this was by editing the definition of the EmployeeData class itself. Now the question is How can I achieve this if I don&#8217;t have the class source code? Well the answer is-you guessed it-Object Initialization Syntax.<br />
Object initialization syntax allows you to create objects with different state(i.e. different values of properties and public variables) quickly and easily. You simply create the instance of your object followed by a comma delimited list of public fields enclosed by two curly brackets ({ }). So instantiating the same John Doe employee using object initialization syntax would look like the following: </span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;"><span style="color:#2b91af;">EmployeeData</span> x = <span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeData</span>() { Name = <span style="color:#a31515;">&#8220;John Doe&#8221;</span>, Age = 25, Position = <span style="color:#a31515;">&#8220;Sales Representatives&#8221;</span> };<br />
ps: you have to define a default constructor in the EmployeeData class in order for the above line to work.</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">You can also call a custom constructor and then use the object initialization syntax afterwards like so:</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;"><span style="color:#2b91af;">EmployeeData</span> x = <span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeData</span>(<span style="color:#a31515;">&#8220;John Doe&#8221;</span>, 25, <span style="color:#a31515;">&#8220;Sales Representative&#8221;</span>) { ID = 1 };</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">In the above example we created the same john doe using the custom constructor then added object initialization syntax to the statement to include the id value to be one. Now the created object has the name &#8220;John Doe&#8221;, Age 25, Position &#8220;Sales Representative&#8221;, ID 1.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">This last code example fires up the question &#8220;what is the order of these values?&#8221;. Consider the following example.<br />
<span style="color:#2b91af;">EmployeeData</span> x = <span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeData</span>(<span style="color:#a31515;">&#8220;John Doe&#8221;</span>, 25, <span style="color:#a31515;">&#8220;Sales Representative&#8221;</span>) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Name = <span style="color:#a31515;">&#8220;Hussien IBS&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Age = 45,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Position = <span style="color:#a31515;">&#8220;CO&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">ID = GUID.NewGuid()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">};</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">Now if you printed out the values of x.Age, x.Name, and x.Position, what will the actual values be? Well one fact to know about object initialization syntax is that it works in order (i.e. x.Name will have the value &#8220;John Doe&#8221; by the custom constructor, then it will be assigned the value &#8220;Brack Belick&#8221; by the syntax that followed the constructor call. Bottom line here: Object initialization syntax gets executed in orderf from left to right.</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">Initializing Inner Types:<br />
Let&#8217;s assume that we have another object in the system called Office like the following: </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">class</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:#2b91af;">Office</span> {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">string</span> OfficeType { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">float</span> OfficeArea { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">and the EmployeeData has an office object as a member of it. Take a look at the following code:<br />
<span style="color:blue;">public</span> <span style="color:#2b91af;">Office</span> MyResidence { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">You can use the object initialization syntax to initialize the inner type as well:<br />
<span style="color:#2b91af;">EmployeeData</span> x = <span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeData</span>() {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Name = <span style="color:#a31515;">&#8220;Husien IBS&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Age = 45,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Position = <span style="color:#a31515;">&#8220;CO&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">ID = <span style="color:#2b91af;">Guid</span>.NewGuid(),</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">MyResidence = <span style="color:blue;">new</span> <span style="color:#2b91af;">Office</span>() {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">OfficeArea = 25.5f,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">OfficeType = <span style="color:#a31515;">&#8220;Glass Office&#8221;</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">};</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">Collection Initializers: </span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">Object initialization syntax allows you to use the array-like initializtion with your types as follows :</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">EmployeeData</span>&gt; enEmps = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">EmployeeData</span>&gt;() {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeData</span>() { </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Position = <span style="color:#a31515;">&#8220;Developer&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Name=<span style="color:#a31515;">&#8220;Mohammed Kasem&#8221;</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}, </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">new</span> <span style="color:#2b91af;">EmployeeData</span>() {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Position = <span style="color:#a31515;">&#8220;Developer&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Name=<span style="color:#a31515;">&#8220;MOhamed Tolba&#8221;</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">};</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;"><br />
&lt;!&#8211;[if !supportLineBreakNewLine]&#8211;&gt;<br />
&lt;!&#8211;[endif]&#8211;&gt;</span></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=49&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2008/11/25/auto-implemented-properties-and-object-initialization-syntax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
		<item>
		<title>C# 3.0 New Features &#8211; What Made LINQ Possible (Part 2). Extension Methods</title>
		<link>http://halwagy.wordpress.com/2008/11/20/c-30-new-features-what-made-linq-possible-part-2/</link>
		<comments>http://halwagy.wordpress.com/2008/11/20/c-30-new-features-what-made-linq-possible-part-2/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 09:52:55 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=38</guid>
		<description><![CDATA[string name = &#8220;Ahmed Halwagy&#8221;; if (name.IsHalwagy()) Console.WriteLine(&#8220;Yeap&#8221;); else Console.WriteLine(&#8220;Not halwagy&#8221;); Shocked? .. Cool! Now I assume that two questions just pumped into your mind: 1- What the heck is that IsHalwagy() method? 2- Would Microsoft ever -for any reason- reshape their string object to add this method? I will answer your questions from bottom [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=38&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:10pt;font-family:&quot;color:blue;">string</span><span style="font-size:10pt;font-family:&quot;"> name = <span style="color:#a31515;">&#8220;Ahmed Halwagy&#8221;</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">if</span> (name.IsHalwagy())</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Yeap&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">else</span> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Not halwagy&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">Shocked? .. Cool!<br />
Now I assume that two questions just pumped into your mind:<br />
1- What the heck is that IsHalwagy() method?<br />
2- Would Microsoft ever -for any reason- reshape their string object to add this method?</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">I will answer your questions from bottom up. Question number 2, the answer is No. Though Microsoft really pursue my own satisfaction, but they never went this far -but I really encourage you to email the C# team and ask them for IsHalwagy() method on the string class(just kidding </span><span style="font-size:10pt;line-height:115%;font-family:Wingdings;"><span> <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></span><span style="font-size:10pt;line-height:115%;font-family:&quot;"> ).</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">Question number 1, the answer is: It’s my own method. I’ve extended the string class by this IsHalwagy() method, and believe it or not, I didn’t have to use classes from the System.Reflection.Emit namespace, actually the job was much easier(as easy as typing 3 lines of code </span><span style="font-size:10pt;line-height:115%;font-family:Wingdings;"><span>J</span></span><span style="font-size:10pt;line-height:115%;font-family:&quot;"> ).</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">Confused! That’s good. Now relax and take a deep breath.<br />
As you know, C# types (classes, interfaces, delegates, structs, enums)once compiled into an assembly, they’re final, meaning that the only way to change them is to have their source code, edit it, and then recompile. However, this is not the case in C# 3.0; in C# 3.0 you can extend any compiled-final- .NET type using a very easy and convenient way through Extension Methods.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Though the usefulness of this capability is extremely obvious in the previous IsHalwagy() example, let’s consider a more useful scenario –at least more useful for you.<br />
&lt;!&#8211;[if !supportLineBreakNewLine]&#8211;&gt;<br />
&lt;!&#8211;[endif]&#8211;&gt;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">static</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">class</span> <span style="color:#2b91af;">IntOperations</span> {<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">bool</span> IsEven(<span style="color:blue;">int</span> x) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> (x % 2) == 0;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">long</span> Factorial(<span style="color:blue;">int</span> x) {<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">if</span> (x &lt;= 1)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> 1;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">else</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> x * Factorial(x &#8211; 1);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">In the previous example we have a class called IntOperations this class contains some of the operations than you would need to perform on any integer. The typical use for the methods in this class would be ass follows:</span></p>
<p class="MsoNormal" style="text-indent:.5in;line-height:normal;margin:0 0 .0001pt .5in;"><span style="font-size:10pt;font-family:&quot;color:blue;">int</span><span style="font-size:10pt;font-family:&quot;"> x = 5;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;The factorial of {0} = {1}&#8221;</span>, x,<span style="color:#2b91af;">IntOperations</span>.Factorial(x));</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#2b91af;">IntOperations</span>.IsEven(5));</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">By using extension methods you can alter the previous example to use the Factorial(int) and the IsEven(int) methods as if they were originally built into the System.Int32 struct. The only change that is required in order to achieve that is to add 8 characters to your methods declaration, as follows:</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;text-indent:.5in;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">static</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">class</span> <span style="color:#2b91af;">IntOperations</span> {<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//Notice the this modifier precedding the parameter declaration.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">bool</span> IsEven(<span style="color:blue;">this</span> <span style="color:blue;">int</span> x) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> (x % 2) == 0;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">long</span> Factorial(<span style="color:blue;">this</span> <span style="color:blue;">int</span> x) {<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">if</span> (x &lt;= 1)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> 1;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">else</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> x * Factorial(x &#8211; 1);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}<span> </span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}<br />
Here all I’ve done is simply adding the “this” modifier before the first parameter of each method declaration. Adding the “this” modifier to the declaration of the int parameter simply instructs the compiler to deal with method as if it was originally built into the System.Int32 type.Voila! that’s it. By doing so you can simply call your methods on any instance of type System.Int32 as if it was originally coded into it:</span></p>
<p class="MsoNormal" style="text-indent:.5in;line-height:normal;margin:0 0 .0001pt .5in;"><span style="font-size:10pt;font-family:&quot;color:blue;">int</span><span style="font-size:10pt;font-family:&quot;"> x = 5;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;The factorial of {0} = {1}&#8221;</span>, x, x.Factorial());</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(x.IsEven());</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Now let’s take alook at what’s really happening behind the scens. If you compile this project and open the assembly using a tool like (ILDASM) you will find that the compiler is invoking the static methods in a very normal manner (i.e. <span style="color:#2b91af;">IntOperations</span>.Factorial(x)). What you really saw of x.Factorial() is just some smoke-and-mirror effect. Here’s a part of the IL viewed by IlDASM for this assembly: </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">.method private hidebysig static void<span> </span>Main(string[] args) cil managed</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">{</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">.entrypoint</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">// Code size<span> </span>44 (0x2c)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">.maxstack<span> </span>3</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">.locals init ([0] int32 x)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0000:<span> </span>nop</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0001:<span> </span>ldc.i4.5</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0002:<span> </span>stloc.0</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0003:<span> </span>ldstr<span> </span>&#8220;The factorial of {0} = {1}&#8221;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0008:<span> </span>ldloc.0</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0009:<span> </span>box<span> </span>[mscorlib]System.Int32</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_000e:<span> </span>ldloc.0</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_000f:<span> </span>call<span> </span>int64 ConsoleApplication3.IntOperations::Factorial(int32) </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0014:<span> </span>box<span> </span>[mscorlib]System.Int64</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0019:<span> </span>call<span> </span>void [mscorlib]System.Console::WriteLine(string,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">object,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">object)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_001e:<span> </span>nop</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_001f:<span> </span>ldloc.0</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0020:<span> </span>call<span> </span>bool ConsoleApplication3.IntOperations::IsEven(int32)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_0025:<span> </span>call<span> </span>void [mscorlib]System.Console::WriteLine(bool)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_002a:<span> </span>nop</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">IL_002b:<span> </span>ret</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10.5pt;font-family:Fixedsys;">} // end of method Program::Main</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Ps: the compiler calls the extension methods statically using their clas names.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Extension Methods Restrictions:</span></p>
<p class="MsoListParagraphCxSpFirst" style="margin-bottom:.0001pt;text-indent:-.25in;line-height:normal;">&lt;!&#8211;[if !supportLists]&#8211;&gt;<span style="font-size:10pt;font-family:&quot;"><span>1-<span style="font-family:&quot;font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;"> </span></span></span>&lt;!&#8211;[endif]&#8211;&gt;<span style="font-size:10pt;font-family:&quot;"> EExtension methods must be declared in static class, and there for they must be static as well (remember static classes can only contain static methods, and static fields).</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin-bottom:.0001pt;text-indent:-.25in;line-height:normal;">&lt;!&#8211;[if !supportLists]&#8211;&gt;<span style="font-size:10pt;font-family:&quot;"><span>2-<span style="font-family:&quot;font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;"> </span></span></span>&lt;!&#8211;[endif]&#8211;&gt;<span style="font-size:10pt;font-family:&quot;"> Normal object instance methods take precedence over extension methods if they happened to be with the same signuature.</span></p>
<p class="MsoListParagraphCxSpLast" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><strong><span style="font-size:10pt;font-family:&quot;">Importing namespaces containing types that define Extension Methods:</span></strong></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><strong></strong></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Namespaces containing the classes that contain extension methods have to imported in order to be able to use the extension methods defined on them.<br />
Let’s take a while discussing this poing, now consider that my IntOperations class is declared inside a namespace called MyEXTNamespace, and the program class containing the main method is declared inside a namespcae called MyAssemblyNamespace. The code will look like the following.<br />
<span style="color:blue;">using</span> System;</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;color:blue;">using</span><span style="font-size:10pt;font-family:&quot;"> System.Linq;</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;color:blue;">namespace</span><span style="font-size:10pt;font-family:&quot;"> MyAssembly {</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">class</span> <span style="color:#2b91af;">Program</span> {</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args) {</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">int</span> x = 5;</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;The factorial of {0} = {1}&#8221;</span>, x, x.Factorial());</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(x.IsEven());</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;">}<span> </span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;color:blue;">namespace</span><span style="font-size:10pt;font-family:&quot;"> MyEXTNamespace {</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">static</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">IntOperations</span> {</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//Notice the this modifier precedding the parameter declaration.</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">bool</span> IsEven(<span style="color:blue;">this</span> <span style="color:blue;">int</span> x) {</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> (x % 2) == 0;</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">long</span> Factorial(<span style="color:blue;">this</span> <span style="color:blue;">int</span> x) {</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">if</span> (x &lt;= 1)</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> 1;</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">else</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> x * Factorial(x &#8211; 1);</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0 0 .0001pt 1.5in;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">If you try to compile this code you will get compile time errors indicating that System.Int32 does not containg a definition fo IsEven() and Factorial(). Can you see it? Can you see the problem?</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">The problem is you declared your extension methods in MyEXTNamespace and in order to use these methods outside the scope of this namespace you have to import this namespace by adding a using directive to MyEXTNamespace namespace at the top of your file as follows: </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">using</span> MyEXTNamespace;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Problem Solved!</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><strong><span style="font-size:10pt;font-family:&quot;">Using Extension Libraries:</span></strong></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Now as you can see it can be very useful if you are able to use your extension methods in many projects as any other .NET library. This is absolutely possible. You can simply move your MyEXTNamespace namespace<span> </span>to another class library project and build this library and simply reference the output assembly from any other .NET project and reuse. Just remember to mark the classes containing the extension methods with the public access modifier in order to be used cross assemblies.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">A Final Detail:</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">One last thing you should know about extension methods is that they apply to inheritance concepty i.e. if you extended<span> </span>a parent class with some method, all the derived classes will inherit this method. Take a look at the following example: </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">string</span> ReturnYourParentType(<span style="color:blue;">this</span> <span style="color:blue;">object</span> o) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Type</span> t = o.GetType();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">if</span> (t.BaseType == <span style="color:blue;">null</span>)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> <span style="color:#a31515;">&#8220;Don&#8217;t have a parent, I&#8217;m object&#8221;</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">return</span> t.BaseType.FullName;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Here I’ve extended the System.Object class with a method that returns the name of the parent type of any object. Now watch me invoking this method: </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">object</span> o = <span style="color:blue;">new</span> <span style="color:blue;">object</span>();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(o.ReturnYourParentType());</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">int</span> x = 5;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//it applies to int too. because int is an object.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">string</span> intParent = x.ReturnYourParentType();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;intParent = {0}&#8221;</span>, intParent);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">// and applies to string too. again because string is also an object.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">string</span> stringParent = <span style="color:#a31515;">&#8220;hello&#8221;</span>.ReturnYourParentType();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;stringParent = {0}&#8221;</span>, stringParent); </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">As you can see here I’ve invoked this method on any thing of type object, and of course in .NET every thing is an object. So because every thing inherits from object, I can call my ReturnYourParentType() on any thing.</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=38&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2008/11/20/c-30-new-features-what-made-linq-possible-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
		<item>
		<title>C# 3.0 New Features &#8211; What made LINQ possible! (part 1)</title>
		<link>http://halwagy.wordpress.com/2008/11/19/c-30-new-features-what-made-linq-possible-part-1/</link>
		<comments>http://halwagy.wordpress.com/2008/11/19/c-30-new-features-what-made-linq-possible-part-1/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 11:34:31 +0000</pubDate>
		<dc:creator>Ahmed</dc:creator>
				<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://halwagy.wordpress.com/?p=34</guid>
		<description><![CDATA[Here&#8217;s a simple problem. Suppose that you have an array of string and you want to filter this array to extract all the items in it that end with the word &#8220;Halwagy&#8221; the code would more or less look like the following. string[] names = { &#8220;Ahmed Halwagy&#8221;, &#8220;Mohamed Halwagy&#8221;, &#8220;Mohamed Fayad&#8221;, &#8220;Walid Hamad&#8221;, &#8220;Ibrahim [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=34&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simple problem. Suppose that you have an array of string and you want to filter this array to extract all the items in it that end with the word &#8220;Halwagy&#8221; the code would more or less look like the following.</p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">string</span><span style="font-size:10pt;font-family:&quot;">[] names = { <span style="color:#a31515;">&#8220;Ahmed Halwagy&#8221;</span>, <span style="color:#a31515;">&#8220;Mohamed Halwagy&#8221;</span>, </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#a31515;">&#8220;Mohamed Fayad&#8221;</span>, <span style="color:#a31515;">&#8220;Walid Hamad&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#a31515;">&#8220;Ibrahim Halwagy&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#a31515;">&#8220;Mohamed Shokry&#8221;</span>,</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#a31515;">&#8220;Amr Badawy&#8221;</span> };</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//TODO: Print all the halwagys in the previous list.<span> </span></span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//1- Traditional approach</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt; result = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt;();</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> s <span style="color:blue;">in</span> names) {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">if</span>(s.EndsWith(<span style="color:#a31515;">&#8220;Halwagy&#8221;</span>))</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">result.Add(s);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> s <span style="color:blue;">in</span> result) <span style="color:#2b91af;">Console</span>.WriteLine(s);</span></p>
<p>I assume that the code listing above is self explanatory, but anyways what&#8217;s happening here is you have this string array &#8220;names&#8221; so you traverse each element in this array and check if it ends with &#8220;Halwagy&#8221;; if so you add it to your result List&lt;string&gt; to eventually manipulate it as you want.</p>
<p>Now take a look at the next syntax (assuming we have the string array “names”):</p>
<p class="MsoNormal" style="text-indent:.5in;line-height:normal;margin:0 0 .0001pt .5in;"><span style="font-size:10pt;font-family:&quot;color:green;">//2- New approach (LINQ)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">var</span> halwagys = <span style="color:blue;">from</span> s <span style="color:blue;">in</span> names</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">where</span> s.EndsWith(<span style="color:#a31515;">&#8220;Halwagy&#8221;</span>)</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">select</span> s;</span></p>
<p><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">foreach</span> (<span style="color:blue;">string</span> s <span style="color:blue;">in</span> halwagys) <span style="color:#2b91af;">Console</span>.WriteLine(s);</span></p>
<p><span style="font-size:10pt;font-family:&quot;">Can you feel it? Can you feel the shift?</span></p>
<p><span style="font-size:10pt;font-family:&quot;">Yes it does, it looks like SQL but it’s not SQL.</span></p>
<p><span style="font-size:10pt;font-family:&quot;">Now I assume that syntax needs a lot of explanation because it really contains a lot of strange words like var, from, where, select, etc.</span></p>
<p><span style="font-size:10pt;font-family:&quot;">So before we digg into an explanation of this statement, let me breifly mention some of the new features in c# 3.0 then we will comeback to this.</span></p>
<p>First thing we will check now is called <span style="color:#7030a0;">Implicitly Typed Local Variables.</span></p>
<p>C# is a strongly typed, static language which means that you can never declare a variable without identifying its data type; otherwise the compiler will generate an error. For example the statement “x = 20;” will generate a compile time error because you haven’t specified the data type of x explicitly to the compiler so to make that work you had to state that x is an integer like so “<span style="font-size:10pt;font-family:&quot;color:blue;">int</span><span style="font-size:10pt;font-family:&quot;"> x = 20;”. Now if you tried to change the value of x to another value that is from a different type (e.g. <span style="color:blue;">float</span>) like “x = 12.5f” the compiler will generate an error now stating that it failed to implicitly convert from <span style="color:blue;">float</span> to <span style="color:blue;">int </span>and an explicit conversion is required. </span></p>
<p class="MsoNormal">Now take a look at the following:<br />
<span style="font-size:10pt;line-height:115%;font-family:&quot;color:blue;">var</span><span style="font-size:10pt;line-height:115%;font-family:&quot;"> x = 20;<br />
what the hek is that? Well appearantly this is not the same as “<span style="color:blue;">int</span> x = 20;”. Actually it is the same, when you declare x as <span style="color:blue;">int</span> the compiler knows that x is of type System.<span style="color:#2b91af;">Int32 </span>and we you declare x as <span style="color:blue;">var </span>the compiler will check the value assigned to x, and will automatically infer the type of x based on the type of that value. so in this case the compiler will be able to infer x is of type System.<span style="color:#2b91af;">Int32</span>, and this happens automatically.</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;"><br />
PS: The type of x will be known in the compile time not in the run time.<br />
&lt;!&#8211;[if !supportLineBreakNewLine]&#8211;&gt;<br />
&lt;!&#8211;[endif]&#8211;&gt;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">You should know that you can use implicit typing (the <span style="color:blue;">var </span>keyword) for any type in the base class library, including arrays, generics, and your custom classes. Take alook at the following examples:<br />
<span style="color:blue;">var</span> x = 12.5f; <span style="color:green;">//x is float.<br />
</span><span style="color:blue;">var</span> list = <span style="color:blue;">new</span> <span style="color:blue;">string</span>[] { <span style="color:#a31515;">&#8220;C#&#8221;</span>, <span style="color:#a31515;">&#8220;LINQ&#8221;</span>, <span style="color:#a31515;">&#8220;.NET 3.0&#8243;</span> }; <span style="color:green;">// list is an array of string<br />
</span><span style="color:blue;">var</span> y = list.Length; <span style="color:green;">//y is int.<br />
</span><span style="color:green;">//using implicit typing in foreach loop.<br />
</span><span style="color:blue;">foreach</span>(<span style="color:blue;">var</span> tech <span style="color:blue;">in</span> list) { <span style="color:green;">//tech is string<br />
</span><span style="color:#2b91af;">Console</span>.WriteLine(tech);<br />
}<br />
Restrictions on Implicit Typing:<br />
1- Implicitly typed variables must be be locals (in a method or a property scope).<br />
2- Local variables declared using <span style="color:blue;">var </span>keyword must be assigned an initial value at the time of declaration, and this value can’t be null. (because, as you may recall, the compiler will infer the type through that value. So the compiler won’t be able to know the sort of type in memory, that the variable will point to by only null).<br />
3- Like any normal strongly typed variable, implicitly typed variables can’t hold a value of a different data type than its original one. </span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><br />
Here’s a code example with the restrictions of the <span style="color:blue;">var </span>keyword:<br />
<span style="color:blue;">class</span> <span style="color:#2b91af;">UnCompilable</span> {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//Error: the contextual keyword var may only appear within local variable declaration</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;"></span><span style="color:blue;">var </span>x<span style="color:green;"> = </span>20<span style="color:green;">;</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"> </p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">void</span> DoSomething() {</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//Error: Implicitly-typed local variables must be initialized.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">var</span> y;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//Error: can&#8217;t assign &lt;null&gt; to an implicitly-typed local variable.</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">var</span> yy = <span style="color:blue;">null</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:blue;">var</span> onlyString = <span style="color:#a31515;">&#8220;Ahmed&#8221;</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">onlyString = <span style="color:#a31515;">&#8220;This is ok&#8221;</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span style="color:green;">//Error: can&#8217;t implicitly convert type int to string. remeber, it&#8217;s a string</span></span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">onlyString = 2;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">}</span></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"><span style="font-size:10pt;line-height:115%;font-family:&quot;">The rest of the features will be posted soon.</span></p>
<p><span style="font-size:10pt;line-height:115%;font-family:&quot;"><br />
</span></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/halwagy.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/halwagy.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/halwagy.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/halwagy.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/halwagy.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/halwagy.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/halwagy.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/halwagy.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/halwagy.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/halwagy.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/halwagy.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/halwagy.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/halwagy.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/halwagy.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=halwagy.wordpress.com&amp;blog=5558986&amp;post=34&amp;subd=halwagy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://halwagy.wordpress.com/2008/11/19/c-30-new-features-what-made-linq-possible-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3b6ccb0d4d2e64616655f3ac8242ca69?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=R" medium="image">
			<media:title type="html">halwagy</media:title>
		</media:content>
	</item>
	</channel>
</rss>
