<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>All things .NET and beyond - C#</title>
    <link>http://www.fluxtah.com/</link>
    <description>Ian (Fluxtah) Warwick's blog</description>
    <language>en-us</language>
    <copyright>Ian Warwick</copyright>
    <lastBuildDate>Mon, 01 Mar 2010 12:26:57 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>fluxtah@hotmail.com</managingEditor>
    <webMaster>fluxtah@hotmail.com</webMaster>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=73917a6b-9f99-4ca2-94fb-d253359594b4</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,73917a6b-9f99-4ca2-94fb-d253359594b4.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:comment>http://www.fluxtah.com/CommentView,guid,73917a6b-9f99-4ca2-94fb-d253359594b4.aspx</wfw:comment>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=73917a6b-9f99-4ca2-94fb-d253359594b4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
First 16 numbers of the fibonacci series
</p>
        <p>
        </p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main(){ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">for</span>(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">long</span> i=0,
n=0, a <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 1,
b <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 0;
i &lt; 16; i++, Console.WriteLine(n), b <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> a,
a <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> n,
n <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> a <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> b);
}</span>
        </pre>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=73917a6b-9f99-4ca2-94fb-d253359594b4" />
      </body>
      <title>Fibonacci in C#</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,73917a6b-9f99-4ca2-94fb-d253359594b4.aspx</guid>
      <link>http://www.fluxtah.com/2010/03/01/FibonacciInC.aspx</link>
      <pubDate>Mon, 01 Mar 2010 12:26:57 GMT</pubDate>
      <description>&lt;p&gt;
First 16 numbers of the fibonacci series
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main(){ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;for&lt;/span&gt;(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;long&lt;/span&gt; i=0,
n=0, a &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 1,
b &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 0;
i &amp;lt; 16; i++, Console.WriteLine(n), b &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; a,
a &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; n,
n &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; a &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; b);
}&lt;/span&gt;&lt;/pre&gt;&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=73917a6b-9f99-4ca2-94fb-d253359594b4" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,73917a6b-9f99-4ca2-94fb-d253359594b4.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=80e5f632-3cf7-435a-b40a-4fe99849a1a4</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,80e5f632-3cf7-435a-b40a-4fe99849a1a4.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:comment>http://www.fluxtah.com/CommentView,guid,80e5f632-3cf7-435a-b40a-4fe99849a1a4.aspx</wfw:comment>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=80e5f632-3cf7-435a-b40a-4fe99849a1a4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I was thinking about this, for some reason, and thought it would be good to remind
myself of those little useful bits of code that you never really use much then sometimes
wonder how to do them.
</p>
        <p>
So my first tidbit, is to test if a number is a whole number, here is my solution,
any better solution than this, answers on a postcard please.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main()
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">decimal</span> decimalNumber <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 4.9M; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">decimal</span> wholeNumber <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 5;
Console.WriteLine(Math.Floor(decimalNumber) == decimalNumber); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
Returns false;</span> Console.WriteLine(Math.Floor(wholeNumber) == wholeNumber); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
Returns true;</span> }</span>
        </pre>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=80e5f632-3cf7-435a-b40a-4fe99849a1a4" />
      </body>
      <title>Testing for a whole number in C#</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,80e5f632-3cf7-435a-b40a-4fe99849a1a4.aspx</guid>
      <link>http://www.fluxtah.com/2009/11/25/TestingForAWholeNumberInC.aspx</link>
      <pubDate>Wed, 25 Nov 2009 10:39:40 GMT</pubDate>
      <description>&lt;p&gt;
I was thinking about this, for some reason, and thought it would be good to remind
myself of those little useful bits of code that you never really use much then sometimes
wonder how to do them.
&lt;/p&gt;
&lt;p&gt;
So my first tidbit, is to test if a number is a whole number, here is my solution,
any better solution than this, answers on a postcard please.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main()
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;decimal&lt;/span&gt; decimalNumber &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 4.9M; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;decimal&lt;/span&gt; wholeNumber &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 5;
Console.WriteLine(Math.Floor(decimalNumber) == decimalNumber); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
Returns false;&lt;/span&gt; Console.WriteLine(Math.Floor(wholeNumber) == wholeNumber); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
Returns true;&lt;/span&gt; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=80e5f632-3cf7-435a-b40a-4fe99849a1a4" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,80e5f632-3cf7-435a-b40a-4fe99849a1a4.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=14c30b90-767b-4c0a-8c14-5354601d5437</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,14c30b90-767b-4c0a-8c14-5354601d5437.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:comment>http://www.fluxtah.com/CommentView,guid,14c30b90-767b-4c0a-8c14-5354601d5437.aspx</wfw:comment>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=14c30b90-767b-4c0a-8c14-5354601d5437</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I wrote a small console application to test if this was possible and it turns out
all you need is an <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlincludeattribute.aspx">XmlInclude</a> attribute
on the base type of the sub-types you are serializing.
</p>
        <p>
In the following example <font face="Courier New">Ninja</font> and <font face="Courier New">Customer</font> inherit
from <font face="Courier New">Person</font>, Person is decorated with the <font face="Courier New">XmlInclude</font> attribute
for both <font face="Courier New">Ninja</font> and <font face="Courier New">Customer</font>.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span>[]
args) { XmlSerializer serializer <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> XmlSerializer(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">typeof</span>(Stuff));
Stuff stuff <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Stuff()
{ People <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> List&lt;Person&gt;()
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Person()
{ Name <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"&lt;b&gt;Bob&lt;/b&gt;"</span>,
Type=<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Person"</span> }, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Ninja()
{ Name <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Hiro"</span>,
Type=<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Ninja"</span>,
Weapon <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Shuriken"</span> }, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Customer()
{ Name <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Fred"</span>,
Type <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Customer"</span>,
CustomerId <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"1234"</span>}
} }; serializer.Serialize(Console.Out, stuff); } } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Stuff
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> List&lt;Person&gt;
People { get; set; } } <strong> [XmlInclude(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">typeof</span>(Ninja)),
XmlInclude(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">typeof</span>(Customer))]</strong><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Person
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> Type
{ get; set; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> Name
{ get; set; } } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Ninja
: Person { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> Weapon
{ get; set; } } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Customer
: Person { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> CustomerId
{ get; set; } }</span>
        </pre>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=14c30b90-767b-4c0a-8c14-5354601d5437" />
      </body>
      <title>C# XML serialization and inheritance</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,14c30b90-767b-4c0a-8c14-5354601d5437.aspx</guid>
      <link>http://www.fluxtah.com/2009/11/13/CXMLSerializationAndInheritance.aspx</link>
      <pubDate>Fri, 13 Nov 2009 07:56:37 GMT</pubDate>
      <description>&lt;p&gt;
I wrote a small console application to test if this was possible and it turns out
all you need is an &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlincludeattribute.aspx"&gt;XmlInclude&lt;/a&gt; attribute
on the base type of the sub-types you are serializing.
&lt;/p&gt;
&lt;p&gt;
In the following example &lt;font face="Courier New"&gt;Ninja&lt;/font&gt; and &lt;font face="Courier New"&gt;Customer&lt;/font&gt; inherit
from &lt;font face="Courier New"&gt;Person&lt;/font&gt;, Person is decorated with the &lt;font face="Courier New"&gt;XmlInclude&lt;/font&gt; attribute
for both &lt;font face="Courier New"&gt;Ninja&lt;/font&gt; and &lt;font face="Courier New"&gt;Customer&lt;/font&gt;.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt;[]
args) { XmlSerializer serializer &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; XmlSerializer(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;typeof&lt;/span&gt;(Stuff));
Stuff stuff &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Stuff()
{ People &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; List&amp;lt;Person&amp;gt;()
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Person()
{ Name &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"&amp;lt;b&amp;gt;Bob&amp;lt;/b&amp;gt;"&lt;/span&gt;,
Type=&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Person"&lt;/span&gt; }, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Ninja()
{ Name &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Hiro"&lt;/span&gt;,
Type=&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Ninja"&lt;/span&gt;,
Weapon &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Shuriken"&lt;/span&gt; }, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Customer()
{ Name &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Fred"&lt;/span&gt;,
Type &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Customer"&lt;/span&gt;,
CustomerId &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"1234"&lt;/span&gt;}
} }; serializer.Serialize(Console.Out, stuff); } } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Stuff
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; List&amp;lt;Person&amp;gt;
People { get; set; } } &lt;strong&gt; [XmlInclude(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;typeof&lt;/span&gt;(Ninja)),
XmlInclude(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;typeof&lt;/span&gt;(Customer))]&lt;/strong&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Person
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; Type
{ get; set; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; Name
{ get; set; } } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Ninja
: Person { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; Weapon
{ get; set; } } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Customer
: Person { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; CustomerId
{ get; set; } }&lt;/span&gt;&lt;/pre&gt;&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=14c30b90-767b-4c0a-8c14-5354601d5437" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,14c30b90-767b-4c0a-8c14-5354601d5437.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=5a3e7a2a-4ccf-4f12-bd1a-8b9a21e0c7ab</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,5a3e7a2a-4ccf-4f12-bd1a-8b9a21e0c7ab.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=5a3e7a2a-4ccf-4f12-bd1a-8b9a21e0c7ab</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Whilst working on my network engine I came across a problem that I initially solved
with an interface, but it turned out that the class that implemented this interface
exposed public methods that I really wanted to be private, an example will explain
the problem. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">interface</span> IMessageReceiver
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Receive(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> message);
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">interface</span> IMessageProvider
{ IMessageReceiver MessageReceiver; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Update();
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> ConcreteReceiver:
IMessageReceiver { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span> IMessageProvider
_MessageProvider; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> ConcreteReciever(IMessageProvider
messageProvider) { _MessageProvider <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> messageProvider;
_MessageProvider.MessageReceiver <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">this</span>;
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Receive(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> message)
{ Console.WriteLine(message); } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Update()
{ MessageProvider.Update(); } } </span>
        </pre>
        <p>
When calling ConcreteReceiver.Update(), this will call MessageProvider.Update(), then,
the concrete implementation of IMessageProvider should call IMessageReceiver.Receive,
effectively like a callback.<br /><br />
Now the issue in my case was that I did not want the Receive method of ConcreteReceiver
to be public, but of course any concrete type that implements an interface must implement
those methods as public, after pondering a while I realised the design was actually
flawed anyway, what I really wanted was a callback, so I changed my design as follows.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">interface</span> IMessageProvider
{ Action&lt;<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span>&gt;
ReceiveAction; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Update();
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> ConcreteReceiver
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span> IMessageProvider
_MessageProvider; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> ConcreteReciever(IMessageProvider
messageProvider) { _MessageProvider <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> messageProvider;
_MessageProvider.ReceiveAction <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> Receive;
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Receive(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> message)
{ Console.WriteLine(message); } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Update()
{ MessageProvider.Update(); } } </span>
        </pre>
        <p>
So now my Receive method on my ConcreteReceiver can be private, since I am now using
a generic Action&lt;T&gt; delegate, this prevents my public API exposing methods that
are really meant to be only used internally. 
</p>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=5a3e7a2a-4ccf-4f12-bd1a-8b9a21e0c7ab" />
      </body>
      <title>Interface or Delegate?</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,5a3e7a2a-4ccf-4f12-bd1a-8b9a21e0c7ab.aspx</guid>
      <link>http://www.fluxtah.com/2009/09/15/InterfaceOrDelegate.aspx</link>
      <pubDate>Tue, 15 Sep 2009 02:50:00 GMT</pubDate>
      <description>&lt;p&gt;
Whilst working on my network engine I came across a problem that I initially solved
with an interface, but it turned out that the class that implemented this interface
exposed public methods that I really wanted to be private, an example will explain
the problem. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;interface&lt;/span&gt; IMessageReceiver
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Receive(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; message);
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;interface&lt;/span&gt; IMessageProvider
{ IMessageReceiver MessageReceiver; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Update();
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; ConcreteReceiver:
IMessageReceiver { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; IMessageProvider
_MessageProvider; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; ConcreteReciever(IMessageProvider
messageProvider) { _MessageProvider &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; messageProvider;
_MessageProvider.MessageReceiver &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;this&lt;/span&gt;;
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Receive(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; message)
{ Console.WriteLine(message); } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Update()
{ MessageProvider.Update(); } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
When calling ConcreteReceiver.Update(), this will call MessageProvider.Update(), then,
the concrete implementation of IMessageProvider should call IMessageReceiver.Receive,
effectively like a callback.&lt;br&gt;
&lt;br&gt;
Now the issue in my case was that I did not want the Receive method of ConcreteReceiver
to be public, but of course any concrete type that implements an interface must implement
those methods as public, after pondering a while I realised the design was actually
flawed anyway, what I really wanted was a callback, so I changed my design as follows.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;interface&lt;/span&gt; IMessageProvider
{ Action&amp;lt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt;&amp;gt;
ReceiveAction; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Update();
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; ConcreteReceiver
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; IMessageProvider
_MessageProvider; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; ConcreteReciever(IMessageProvider
messageProvider) { _MessageProvider &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; messageProvider;
_MessageProvider.ReceiveAction &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; Receive;
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Receive(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; message)
{ Console.WriteLine(message); } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Update()
{ MessageProvider.Update(); } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
So now my Receive method on my ConcreteReceiver can be private, since I am now using
a generic Action&amp;lt;T&amp;gt; delegate, this prevents my public API exposing methods that
are really meant to be only used internally. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=5a3e7a2a-4ccf-4f12-bd1a-8b9a21e0c7ab" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,5a3e7a2a-4ccf-4f12-bd1a-8b9a21e0c7ab.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=39d8048b-31ee-4d9f-aae6-65648c0a75bc</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,39d8048b-31ee-4d9f-aae6-65648c0a75bc.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:comment>http://www.fluxtah.com/CommentView,guid,39d8048b-31ee-4d9f-aae6-65648c0a75bc.aspx</wfw:comment>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=39d8048b-31ee-4d9f-aae6-65648c0a75bc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The Available property of the System.Net.Sockets class will tell you how much data
is available to read. 
</p>
        <p>
With UDP sockets, one thing to remember is that Socket.Available will give the total
size of all the datagrams ready to read, so to the only way to know how many datagrams
are waiting to be read is to call ReceiveFrom repeatedly until all data is read, for
instance:- 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">while</span>(Socket.Available
&gt; 0) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">int</span> datagramSize <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> Socket.ReceiveFrom(buffer, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">ref</span> endPoint);
} </span>
        </pre>
        <p>
          <font face="verdana,geneva">The datagramSize variable will give the size of the datagram
that was read, this can be troublesome to manage since you do not know what you are
going to get, until you get it, so you would need to initialize a large enough buffer
to hold the datagram.</font>
        </p>
        <p>
In the networking framework I am currently writing, the application has a configurable
MaxPacketSize option so I can initialize my buffer to this size, but this wont help
in the event that a bum packet is sent that breaches this constraint so some error
handling would also need to be in place to compensate for this issue. 
</p>
        <p>
        </p>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=39d8048b-31ee-4d9f-aae6-65648c0a75bc" />
      </body>
      <title>Socket.Available and UDP</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,39d8048b-31ee-4d9f-aae6-65648c0a75bc.aspx</guid>
      <link>http://www.fluxtah.com/2009/09/09/SocketAvailableAndUDP.aspx</link>
      <pubDate>Wed, 09 Sep 2009 16:26:00 GMT</pubDate>
      <description>&lt;p&gt;
The Available property of the System.Net.Sockets class will tell you how much data
is available to read. 
&lt;/p&gt;
&lt;p&gt;
With UDP sockets, one thing to remember is that Socket.Available will give the total
size of all the datagrams ready to read, so to the only way to know how many datagrams
are waiting to be read is to call ReceiveFrom repeatedly until all data is read, for
instance:- 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;while&lt;/span&gt;(Socket.Available
&amp;gt; 0) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;int&lt;/span&gt; datagramSize &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; Socket.ReceiveFrom(buffer, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;ref&lt;/span&gt; endPoint);
} &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;font face=verdana,geneva&gt;The datagramSize variable will give the size of the datagram
that was read, this can be troublesome to manage since you do not know what you are
going to get, until you get it, so you would need to initialize a large enough buffer
to hold the datagram.&lt;/font&gt; 
&lt;/p&gt;
&lt;p&gt;
In the networking framework I am currently writing, the application has a configurable
MaxPacketSize option so I can initialize my buffer to this size, but this wont help
in the event that a bum packet is sent that breaches this constraint so some error
handling would also need to be in place to compensate for this issue. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=39d8048b-31ee-4d9f-aae6-65648c0a75bc" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,39d8048b-31ee-4d9f-aae6-65648c0a75bc.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=c55d960a-edb3-49a9-842d-7efac7017454</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,c55d960a-edb3-49a9-842d-7efac7017454.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=c55d960a-edb3-49a9-842d-7efac7017454</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Another keyword I have not used much is the new modifer, the <a title="MSDN docs for the new Modifer " href="http://msdn.microsoft.com/en-gb/library/435f1dw2.aspx">MSDN
docs for the new Modifer</a> open up with the following:- 
</p>
        <p>
          <em>When used as a modifier, the new keyword explicitly hides a member inherited from
a base class. When you hide an inherited member, the derived version of the member
replaces the base-class version. Although you can hide members without the use of
the new modifier, the result is a warning. If you use new to explicitly hide a member,
it suppresses this warning and documents the fact that the derived version is intended
as a replacement.</em>
        </p>
        <p>
The new modifier has an interesting but important effect when casting, as the
following example demonstrates. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Animal
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Say()
{ Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"I
am an animal!"</span>); } } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Cat
: Animal { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Say()
{ Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Meeeoooooww!"</span>);
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> WildCat
: Cat { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Say()
{ Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"ROAR!"</span>);
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main()
{ WildCat wildCat <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> WildCat();
Cat cat <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> wildCat;
Animal animal <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cat;
wildCat.Say(); cat.Say(); animal.Say(); } } </span>
        </pre>
        <p>
The code outputs the following. 
</p>
        <pre>ROAR!<br />
Meeeoooooww!<br />
I am an animal!  </pre>
        <p>
The code demonstrates when downcasting, WildCat and Cat use their own implementations
of Say(), I am not sure I would want that type of behaviour, since it could be rather
dangerous if its not used intentionally, but its interesting to know anyway. 
</p>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=c55d960a-edb3-49a9-842d-7efac7017454" />
      </body>
      <title>C# new Modifier</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,c55d960a-edb3-49a9-842d-7efac7017454.aspx</guid>
      <link>http://www.fluxtah.com/2009/03/05/CNewModifier.aspx</link>
      <pubDate>Thu, 05 Mar 2009 11:28:00 GMT</pubDate>
      <description>&lt;p&gt;
Another keyword I have not used much is the new modifer, the &lt;a title="MSDN docs for the new Modifer " href="http://msdn.microsoft.com/en-gb/library/435f1dw2.aspx"&gt;MSDN
docs for the new Modifer&lt;/a&gt; open up with the following:- 
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;When used as a modifier, the new keyword explicitly hides a member inherited from
a base class. When you hide an inherited member, the derived version of the member
replaces the base-class version. Although you can hide members without the use of
the new modifier, the result is a warning. If you use new to explicitly hide a member,
it suppresses this warning and documents the fact that the derived version is intended
as a replacement.&lt;/em&gt; 
&lt;/p&gt;
&lt;p&gt;
The new modifier&amp;nbsp;has an interesting but important effect when casting, as the
following example demonstrates.&amp;nbsp;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Animal
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Say()
{ Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"I
am an animal!"&lt;/span&gt;); } } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Cat
: Animal { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Say()
{ Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Meeeoooooww!"&lt;/span&gt;);
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; WildCat
: Cat { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Say()
{ Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"ROAR!"&lt;/span&gt;);
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main()
{ WildCat wildCat &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; WildCat();
Cat cat &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; wildCat;
Animal animal &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cat;
wildCat.Say(); cat.Say(); animal.Say(); } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The code outputs the following. 
&lt;/p&gt;
&lt;pre&gt;ROAR!&lt;br&gt;
Meeeoooooww!&lt;br&gt;
I am an animal!&amp;nbsp; &lt;/pre&gt;
&lt;p&gt;
The code demonstrates when downcasting,&amp;nbsp;WildCat and&amp;nbsp;Cat use their own implementations
of Say(), I am not sure I would want that type of behaviour, since it could be rather
dangerous if its not used intentionally, but its interesting to know anyway. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=c55d960a-edb3-49a9-842d-7efac7017454" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,c55d960a-edb3-49a9-842d-7efac7017454.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=7e6ed50d-7114-4db7-b83a-b82c57299ffa</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,7e6ed50d-7114-4db7-b83a-b82c57299ffa.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=7e6ed50d-7114-4db7-b83a-b82c57299ffa</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">abstract</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Animal{} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Cat
: Animal {} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Ornament
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">explicit</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">operator</span> Ornament(Cat
cat) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Ornament();
} } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main()
{ Cat cat <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Cat(); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
This line fails to compile</span> Ornament jug <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cat <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">as</span> Ornament; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
this works with our explicit operator defined for Ornament</span> Ornament anotherJug <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> (Ornament)cat;
} } </span>
        </pre>
        <p>
Well the holes in my C# knowledge never cease to amaze me, I mean, I knew what I could
do with the <strong>as</strong> keyword but I did not know the subtle difference it
has to a direct cast.<br /><br />
By using the as keyword to cast from one type to another, if the cast is not possible
it will result in a null, however using a direct cast will throw an exception. 
</p>
        <p>
An example demonstrates the use of the as keyword. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">abstract</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Animal{} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Cat
: Animal{} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Ornament{} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main()
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">object</span> cat <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> Cat(); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
jug will be null after this conversion since</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
cat is not an Ornament</span> Ornament jug <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cat <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">as</span> Ornament; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
this throws an InvalidCastException</span> jug <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> (Ornament)
cat; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
This works since Cat is an Animal, animal will not</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
have a reference to cat</span> Animal animal <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> cat <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">as</span> Animal;
} } </span>
        </pre>
        <p>
Quite handy really, but there is one thing to note, notice that we say <strong>object
cat = new Cat();</strong>? This is because we get a compilation error if we had used <strong>Cat
cat = new Cat();</strong><br /><br /><em>Cannot convert type 'ConsoleApplication1.Cat' to 'ConsoleApplication1.Ornament'
via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion,
or null type conversion</em></p>
        <p>
The reason for this (I believe) is because of the limitation described in
the <a title="MSDN Docs for the as keyword" href="http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx">MSDN
Docs for the as keyword</a>. 
</p>
        <p>
          <em>Note that the as operator only performs reference conversions and boxing conversions.
The as operator cannot perform other conversions, such as user-defined conversions,
which should instead be performed by using cast expressions.</em>
        </p>
        <p>
An example below shows this, it has been slightly modified by adding an explicit
cast operator to Ornament so we can use a cast expression for <strong>anotherJug</strong> to
show the difference between a cast and a conversion with the as keyword, so as
the docs say:- user-defined conversions do not work with the as keyword. 
</p>
        <p>
        </p>
        <a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.fluxtah.com%2fpost%2fC-as-keyword.aspx">
          <img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.fluxtah.com%2fpost%2fC-as-keyword.aspx" width="82" height="18" />
        </a>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=7e6ed50d-7114-4db7-b83a-b82c57299ffa" />
      </body>
      <title>C# as keyword</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,7e6ed50d-7114-4db7-b83a-b82c57299ffa.aspx</guid>
      <link>http://www.fluxtah.com/2009/03/05/CAsKeyword.aspx</link>
      <pubDate>Thu, 05 Mar 2009 06:01:00 GMT</pubDate>
      <description>&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;abstract&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Animal{} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Cat
: Animal {} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Ornament
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;explicit&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;operator&lt;/span&gt; Ornament(Cat
cat) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Ornament();
} } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main()
{ Cat cat &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Cat(); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
This line fails to compile&lt;/span&gt; Ornament jug &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cat &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;as&lt;/span&gt; Ornament; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
this works with our explicit operator defined for Ornament&lt;/span&gt; Ornament anotherJug &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; (Ornament)cat;
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Well the holes in my C# knowledge never cease to amaze me, I mean, I knew what I could
do with the &lt;strong&gt;as&lt;/strong&gt; keyword but I did not know the subtle difference it
has to a direct cast.&lt;br&gt;
&lt;br&gt;
By using the as keyword to cast from one type to another, if the cast is not possible
it will result in a null, however using a direct cast will throw an exception. 
&lt;/p&gt;
&lt;p&gt;
An example demonstrates the use of the&amp;nbsp;as keyword. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;abstract&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Animal{} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Cat
: Animal{} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Ornament{} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main()
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;object&lt;/span&gt; cat &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; Cat(); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
jug will be null after this conversion since&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
cat is not an Ornament&lt;/span&gt; Ornament jug &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cat &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;as&lt;/span&gt; Ornament; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
this throws an InvalidCastException&lt;/span&gt; jug &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; (Ornament)
cat; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
This works since Cat is an Animal, animal will not&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
have a reference to cat&lt;/span&gt; Animal animal &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; cat &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;as&lt;/span&gt; Animal;
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Quite handy really, but there is one thing to note, notice that we say &lt;strong&gt;object
cat = new Cat();&lt;/strong&gt;? This is because we get a compilation error if we had used &lt;strong&gt;Cat
cat = new Cat();&lt;/strong&gt; 
&lt;br&gt;
&lt;br&gt;
&lt;em&gt;Cannot convert type 'ConsoleApplication1.Cat' to 'ConsoleApplication1.Ornament'
via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion,
or null type conversion&lt;/em&gt; 
&lt;/p&gt;
&lt;p&gt;
The reason for this (I&amp;nbsp;believe) is because of the limitation described&amp;nbsp;in
the &lt;a title="MSDN Docs for the as keyword" href="http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx"&gt;MSDN
Docs for the as keyword&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Note that the as operator only performs reference conversions and boxing conversions.
The as operator cannot perform other conversions, such as user-defined conversions,
which should instead be performed by using cast expressions.&lt;/em&gt; 
&lt;/p&gt;
&lt;p&gt;
An example below shows this,&amp;nbsp;it has been slightly modified by adding an explicit
cast operator to Ornament so we can use&amp;nbsp;a cast expression for &lt;strong&gt;anotherJug&lt;/strong&gt; to
show the difference between&amp;nbsp;a cast and a conversion with the as keyword, so as
the docs say:- user-defined conversions do not work with the as keyword. 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.fluxtah.com%2fpost%2fC-as-keyword.aspx"&gt;&lt;img border=0 alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.fluxtah.com%2fpost%2fC-as-keyword.aspx" width=82 height=18&gt;&lt;/a&gt; &lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=7e6ed50d-7114-4db7-b83a-b82c57299ffa" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,7e6ed50d-7114-4db7-b83a-b82c57299ffa.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=81a2fff6-eecc-4890-a239-f71276b5d1af</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,81a2fff6-eecc-4890-a239-f71276b5d1af.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=81a2fff6-eecc-4890-a239-f71276b5d1af</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Well, smack me with a wet kipper, I have never really considered the difference between
do and do while, since I have always just used a while loop. 
</p>
        <p>
Anyway, the difference is, that a do-while will always execute before checking the
condtion, whereas a while loop, checks the condition before executing the code block. 
</p>
        <p>
Quite a fundamental difference that could prove to be useful, I definetly see a 'gotcha!'
interview question from this one if it was ever asked. 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main()
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">int</span> x <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 0; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
Will always output "Hello World"</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">do</span> {
Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Hello
World"</span>); x++; } <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">while</span> (x
&lt;= 0); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
Will not output anything</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">while</span> (x
&lt;= 0) { Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Hello
World"</span>); x++; } } } </span>
        </pre>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=81a2fff6-eecc-4890-a239-f71276b5d1af" />
      </body>
      <title>C# do vs do while</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,81a2fff6-eecc-4890-a239-f71276b5d1af.aspx</guid>
      <link>http://www.fluxtah.com/2009/03/05/CDoVsDoWhile.aspx</link>
      <pubDate>Thu, 05 Mar 2009 04:40:00 GMT</pubDate>
      <description>&lt;p&gt;
Well, smack me with a wet kipper, I have never really considered the difference between
do and do while, since I have always just used a while loop. 
&lt;/p&gt;
&lt;p&gt;
Anyway, the difference is, that a do-while will always execute before checking the
condtion, whereas a while loop, checks the condition before executing the code block. 
&lt;/p&gt;
&lt;p&gt;
Quite a fundamental difference that could prove to be useful, I definetly see a 'gotcha!'
interview question from this one if it was ever asked. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main()
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;int&lt;/span&gt; x &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 0; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
Will always output "Hello World"&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;do&lt;/span&gt; {
Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Hello
World"&lt;/span&gt;); x++; } &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;while&lt;/span&gt; (x
&amp;lt;= 0); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
Will not output anything&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;while&lt;/span&gt; (x
&amp;lt;= 0) { Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Hello
World"&lt;/span&gt;); x++; } } } &lt;/span&gt;&lt;/pre&gt;&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=81a2fff6-eecc-4890-a239-f71276b5d1af" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,81a2fff6-eecc-4890-a239-f71276b5d1af.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=a2ffe639-26e0-4552-884b-5496808ddde3</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,a2ffe639-26e0-4552-884b-5496808ddde3.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=a2ffe639-26e0-4552-884b-5496808ddde3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I had a great job interview for a great company on tuesday just gone, they are
a Microsoft Gold Certified Partner so that suits me down to the ground since I am
a Microsoft fanboy! I have a second stage interview with them that will be technical,
so it would probably involve a test and lots of questions, so I have decided to put
in some more study into my profession and brush up on stuff, starting with C#. 
</p>
        <p>
I think myself as a fairly good C# programmer, not the best, but fairly good, but,
there are some things that I have never needed to learn, in this post I will look
at the <strong>checked</strong> keyword. The checked keyword is used to control the
overflow-checking context of integral-type arithmetic operations and conversions - <a title="msdn checked keyword" href="http://msdn.microsoft.com/en-us/library/74b4xzyw(VS.71).aspx" target="_blank">cited
from the msdn docs</a>. 
</p>
        <p>
At first glance, since some things are not always clear to me, my thoughts are - what
does this mean? 
</p>
        <p>
Well, if we take a byte, we know that a byte can only have maximum value of 255, so
any greater value than 255 would result in an overflow. 
</p>
        <p>
So what checked does is - if you perform an arithmetic operation that results
in an overflow, it will throw an OverflowException, the following example shows this.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main()
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">byte</span> a <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 200; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">byte</span> b <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 200; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">byte</span> c <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">checked</span>((<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">byte</span>)(a <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">+</span> b));
Console.WriteLine(c); } } </span>
        </pre>
        <p>
Without the checked, the calculation would wrap giving c a value of 144. 
</p>
        <p>
I have never needed to use this yet, but I guess if an important calculation was being
performed then I would consider it, for instance if it was going to lose a lot of
money! 
</p>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=a2ffe639-26e0-4552-884b-5496808ddde3" />
      </body>
      <title>C# checked keyword</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,a2ffe639-26e0-4552-884b-5496808ddde3.aspx</guid>
      <link>http://www.fluxtah.com/2009/03/05/CCheckedKeyword.aspx</link>
      <pubDate>Thu, 05 Mar 2009 03:43:00 GMT</pubDate>
      <description>&lt;p&gt;
I had a great job interview for a great company&amp;nbsp;on tuesday just gone, they are
a Microsoft Gold Certified Partner so that suits me down to the ground since I am
a Microsoft fanboy! I have a second stage interview with them that will be technical,
so it would probably involve a test and lots of questions, so I have decided to put
in some more study into my profession and brush up on stuff, starting with C#. 
&lt;/p&gt;
&lt;p&gt;
I think myself as a fairly good C# programmer, not the best, but fairly good, but,
there are some things that I have never needed to learn, in this post I will look
at the &lt;strong&gt;checked&lt;/strong&gt; keyword. The checked keyword is used to control the
overflow-checking context of integral-type arithmetic operations and conversions - &lt;a title="msdn checked keyword" href="http://msdn.microsoft.com/en-us/library/74b4xzyw(VS.71).aspx" target=_blank&gt;cited
from the msdn docs&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
At first glance, since some things are not always clear to me, my thoughts are&amp;nbsp;-&amp;nbsp;what
does this mean? 
&lt;/p&gt;
&lt;p&gt;
Well, if we take a byte, we know that a byte can only have maximum value of 255, so
any greater value than 255 would result in an overflow. 
&lt;/p&gt;
&lt;p&gt;
So what checked does is&amp;nbsp;-&amp;nbsp;if you perform an arithmetic operation that results
in an overflow, it will throw an OverflowException, the following example shows this.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main()
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;byte&lt;/span&gt; a &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 200; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;byte&lt;/span&gt; b &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 200; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;byte&lt;/span&gt; c &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;checked&lt;/span&gt;((&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;byte&lt;/span&gt;)(a &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;+&lt;/span&gt; b));
Console.WriteLine(c); } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Without the checked, the calculation would wrap giving c a value of 144. 
&lt;/p&gt;
&lt;p&gt;
I have never needed to use this yet, but I guess if an important calculation was being
performed then I would consider it, for instance if it was going to lose a lot of
money! 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=a2ffe639-26e0-4552-884b-5496808ddde3" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,a2ffe639-26e0-4552-884b-5496808ddde3.aspx</comments>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.fluxtah.com/Trackback.aspx?guid=1ca2b064-0922-456e-a554-0744e111bc37</trackback:ping>
      <pingback:server>http://www.fluxtah.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.fluxtah.com/PermaLink,guid,1ca2b064-0922-456e-a554-0744e111bc37.aspx</pingback:target>
      <dc:creator>Fluxtah</dc:creator>
      <wfw:commentRss>http://www.fluxtah.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1ca2b064-0922-456e-a554-0744e111bc37</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Recently at a job interview during a technical test I had to answer a question: 
</p>
        <p>
          <em>What is the difference between a delegate and a multicast delegate?</em>
        </p>
        <p>
Unfortunately I did not have the answer, I have used delegates plenty but I have never
obviously used them enough to know this difference, and the dissapointing thing is
that the answer was glaringly obvious. 
</p>
        <p>
A multicast delegate is multiple instances of the same delegate chained together using
the + operator, Events in .NET make use of this to attach multiple event handlers
to an event. 
</p>
        <p>
An example: 
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">delegate</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> SaySomething(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> something); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span>[]
args) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
At first, something is just a delegate</span> SaySomething something <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> SaySomething((s)
=&gt; Console.WriteLine(s)); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
By adding together more instances of the SaySomething delegate.</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">//
we get a multicast delegate</span> something += <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> SaySomething((s)
=&gt; Console.WriteLine(s.ToUpper())); something += <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> SaySomething((s)
=&gt; Console.WriteLine(s.ToLower())); something(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Hello"</span>);
} } </span>
        </pre>
        <p>
The output would give: 
</p>
        <p>
Hello<br />
HELLO<br />
hello 
</p>
        <p>
It pains me to think I have now been at this for 6 years and there are still aspects
to C# that I have not yet come across, yet I do not think of myself as a bad developer,
I am sure there are plenty of things that I am aware of that the interviewer has no
idea about, and it makes me wonder if the whole point is to see areas of where I could
improve my knowledge, or the purpose is to scrutinise my knowledge in a way that an
incorrect answer would make me out to be some kind of fraud. 
</p>
        <p>
I guess at least now if this question comes up again I will be able to answer it. 
</p>
        <img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=1ca2b064-0922-456e-a554-0744e111bc37" />
      </body>
      <title>Delegates vs Multicast Delegates</title>
      <guid isPermaLink="false">http://www.fluxtah.com/PermaLink,guid,1ca2b064-0922-456e-a554-0744e111bc37.aspx</guid>
      <link>http://www.fluxtah.com/2009/02/12/DelegatesVsMulticastDelegates.aspx</link>
      <pubDate>Thu, 12 Feb 2009 06:20:00 GMT</pubDate>
      <description>&lt;p&gt;
Recently at a job interview during a technical test I had to answer a question: 
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;What is the difference between a delegate and a multicast delegate?&lt;/em&gt; 
&lt;/p&gt;
&lt;p&gt;
Unfortunately I did not have the answer, I have used delegates plenty but I have never
obviously used them enough to know this difference, and the dissapointing thing is
that the answer was glaringly obvious. 
&lt;/p&gt;
&lt;p&gt;
A multicast delegate is multiple instances of the same delegate chained together using
the + operator, Events in .NET make use of this to attach multiple event handlers
to an event. 
&lt;/p&gt;
&lt;p&gt;
An example:&amp;nbsp;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;delegate&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; SaySomething(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; something); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt;[]
args) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
At first, something is just a delegate&lt;/span&gt; SaySomething something &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; SaySomething((s)
=&amp;gt; Console.WriteLine(s)); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
By adding together more instances of the SaySomething delegate.&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;//
we get a multicast delegate&lt;/span&gt; something += &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; SaySomething((s)
=&amp;gt; Console.WriteLine(s.ToUpper())); something += &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; SaySomething((s)
=&amp;gt; Console.WriteLine(s.ToLower())); something(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Hello"&lt;/span&gt;);
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The output would give: 
&lt;/p&gt;
&lt;p&gt;
Hello&lt;br&gt;
HELLO&lt;br&gt;
hello 
&lt;/p&gt;
&lt;p&gt;
It pains me to think I have now been at this for 6 years and there are still aspects
to C# that I have not yet come across, yet I do not think of myself as a bad developer,
I am sure there are plenty of things that I am aware of that the interviewer has no
idea about, and it makes me wonder if the whole point is to see areas of where I could
improve my knowledge, or the purpose is to scrutinise my knowledge in a way that an
incorrect answer would make me out to be some kind of fraud. 
&lt;/p&gt;
&lt;p&gt;
I guess at least now if this question comes up again I will be able to answer it. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.fluxtah.com/aggbug.ashx?id=1ca2b064-0922-456e-a554-0744e111bc37" /&gt;</description>
      <comments>http://www.fluxtah.com/CommentView,guid,1ca2b064-0922-456e-a554-0744e111bc37.aspx</comments>
      <category>C#</category>
    </item>
  </channel>
</rss>