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

<channel>
	<title>Martijn's C# Programming Blog &#187; language</title>
	<atom:link href="http://www.dijksterhuis.org/tag/language/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Uncommon C# : Using C style unions in C#</title>
		<link>http://www.dijksterhuis.org/uncommon-style-unions/</link>
		<comments>http://www.dijksterhuis.org/uncommon-style-unions/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 02:16:47 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Uncommon C#]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=672</guid>
		<description><![CDATA[
A union is a data structure that stores several different types of data at the same single location in memory. Although this might sound like madness in a garbage collected world many older binary data formats still heavily rely on this. A possible use is if you want to modify an IPv4 address. An IPv4 [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2009/02/rings1.jpg" alt="Uncommon unions in C#" title="Uncommon unions in C#" width="500" height="226" class="alignnone size-full wp-image-676" /></p>
<p><em>A union is a data structure that stores several different types of data at the same single location in memory. Although this might sound like madness in a garbage collected world many older binary data formats still heavily rely on this. A possible use is if you want to modify an IPv4 address. An IPv4 address is 4 bytes, or an unsigned C# int. You might want to manipulate the IP address as a single value, or by splitting it into four separate bytes.</em></p>
<p><span id="more-672"></span></p>
<p>C# does not have a union keyword like C or C++ but it is still possible to create the same result by using the StructLayout(LayoutKind.Explicit) and FieldOffset attributes which are part of System.Runtime.InteropServices (which as the name suggests supplies code for working with unmanaged code).</p>
<p>C# allows for three distinct ways of layout data structures in memory: Auto, Explicit, and Sequential. The default is &#8220;Auto&#8221;, but for this to work we need &#8220;Explicit&#8221;. </p>
<p>The FieldOffset sets the physical position of fields within the representation of a class or structure. But what it also allow you to do is to set the same position twice. In the below example we will let the unsigned int for the IP address start at the same spot as the most significant byte.</p>
<pre class="brush: c#">
using System;
using System.Runtime.InteropServices;

namespace ipv4
{

    [StructLayout(LayoutKind.Explicit)]
    struct IPv4Address
    {
        [FieldOffset(0)]
        public uint Address;
        [FieldOffset(0)]
        public byte b3;
        [FieldOffset(1)]
        public byte b2;
        [FieldOffset(2)]
        public byte b1;
        [FieldOffset(3)]
        public byte b0;
    }

    class MainClass
    {
        public static void Main(string[] args)
        {
            IPv4Address myAddress;

            // Assign localhost to the IPv4 address
            myAddress.Address = 0; // Avoid CS0170: Unassigned Field error
            myAddress.b0 = 127;
            myAddress.b1 = 0;
            myAddress.b2 = 0;
            myAddress.b3 = 1;

            Console.WriteLine(&quot;The address in hexadecimal: {0:x}&quot;,myAddress.Address);
        }
    }
}
</pre>
<p>Image credit: <a rel="nofollow" href="http://www.flickr.com/photos/jeffbelmonte/">Jeff Belmonte</a></p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/uncommon-style-unions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# Preprocessor Directives Explained</title>
		<link>http://www.dijksterhuis.org/csharp-preprocessor-directives-explained/</link>
		<comments>http://www.dijksterhuis.org/csharp-preprocessor-directives-explained/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 09:09:42 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[preprocessor directives]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=653</guid>
		<description><![CDATA[By using pre-processor directives you can exclude parts of your code from being seen by the compiler. Excluded from the assembly they are never seen at run-time.  This is different from a regular if (x) {}  block where code is actually compiled in, evaluated at runtime and added to the assembly.
To understand why [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p>By using pre-processor directives you can exclude parts of your code from being seen by the compiler. Excluded from the assembly they are never seen at run-time.  This is different from a regular<em> if (x) {} </em> block where code is actually compiled in, evaluated at runtime and added to the assembly.</p>
<p>To understand why you would want to do this, a little history: One of the good things about C is that it works on every imaginable platform, the bad thing was of course that it works on every imaginable platform. There was always a little tweaking required to get your code to compile. Your program might have needed to compile on Amiga, DOS , OS/2, Windows or Linux. The invention of the preprocessor made this much easier. As a separate step prior to compilation it combs through the source code and modifies it according to the pre-processing instructions found in the source code. The C compiler never gets to see the things that don&#8217;t apply to it.</p>
<p><span id="more-653"></span></p>
<p>In C# there is no separate preprocessor step instead the compiler itself skims through the code as it reads it. Fortunately it is also nowhere near as complex as the C pre-processor.</p>
<p><strong>Contents</strong></p>
<ul>
<li><a href="#A">The #define and #undef directives</a></li>
<li><a href="#B">Unlike C and C++ there are no macros in C#</a></li>
<li><a href="#C">Conditional Compilation with #if / #else / #endif</a></li>
<li><a href="#D">Avoid the clutter &#8211; use conditional attributes instead</a></li>
<li><a href="#E">Using #error and #warning during compilation</a></li>
<li><a href="#F">Generated code builds and error reporting with #line</a></li>
<li><a href="#G">Region Directives</a></li>
</ul>
<p><a name="a"></a><strong>The #define and #undef directives</strong></p>
<p>The basis of the C# preprocessor directives are #define and #undef. They allow you to create and destroy symbols used by the preprocessing step.</p>
<blockquote><p>#define TESTVERSION<br />
#undef  DEBUG</p></blockquote>
<p>A common use is for a programmer to turn on and off the inclusion of debugging code. When you are coding and debugging this allow you to test your code but this code shouldn&#8217;t be present in the production version of your software.</p>
<p>It is possible to set symbols using <em>/define</em> on the compilers command line allowing you to build scripts that produce different versions of your code. For example a &#8220;full version&#8221; and a limited &#8220;demo&#8221; version.</p>
<p><strong>C# does not have any pre-defined symbols, except for maybe DEBUG</strong></p>
<p>If you are used to your C/C++ compiler <a rel="nofollow" href="http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html">setting large numbers of symbols</a> so that you could identify the compiler and version you will puzzled to discover the C# doesn&#8217;t have any. So you cannot check for the .NET version from a pre-defined symbol and branch your code based on that. (The closest that you can do is to check for the execution environment at runtime, for example if you want to know <a rel="nofollow" href="http://www.mono-project.com/FAQ:_Technical#How_can_I_detect_if_am_running_in_Mono.3F">if you a running under mono</a>.)</p>
<p>If you are compiling your code in the Visual C# / MonoDevelop environment there will be one symbol defined you can use: DEBUG. This is set in the project options and if you are building your code you can select if you want to build the Debug or Release version.</p>
<p><a name="B"></a><strong>Unlike C and C++ there are no macros in C#</strong></p>
<p>The following is not going to fly on C#. Macro&#8217;s are commonly used to expand expressions while compiling C code. A common problem is that C doesn&#8217;t have a boolean type. So the first thing most programmers do when starting a new project is to define their own as shown in the next little C code snippet:</p>
<pre class="brush: c">
#include &lt;stdio.h&gt;

#define BOOL  int
#define TRUE   1
#define FALSE  0
#define IS_TRUE(x) (x == TRUE)

void main()
{
        if (IS_TRUE(TRUE))
        {
          printf(&quot;It is TRUE!&quot;);
        }
}
</pre>
<p>When the C preprocessor comes across this it would replace &#8220;BOOL&#8221; with int, FALSE with 0 and TRUE with 1. The compiler never saw the BOOL, or the TRUE or FALSE. But lets not hang around here &#8212; as said, this is not supported by C#</p>
<p><a name="C"></a><strong>Conditional Compilation with #if / #else / #endif</strong></p>
<p>Of course, if you can define symbols you need to be able to test for them as well. The #if / #else / #endif directives do just that. If you are building the debug version of your code, you can check for the DEBUG symbol:</p>
<blockquote><p>#if DEBUG<br />
            Console.WriteLine(&#8221;Things are not going so well!&#8221;);<br />
#endif
</p></blockquote>
<p>If you are not building the Debug version of your code &#8212; the above line is never seen by the compiler, and thus is not included in the assembly. All #if statements need to be closed by an #endif, and of course you can also use an #else statement:</p>
<blockquote><p>
#if DEBUG<br />
            Console.WriteLine(&#8221;Things are not going so well, some more debugging is needed!&#8221;);<br />
#else<br />
            Console.WriteLine(&#8221;Fatal Error: Please call free support (0800) 123 123&#8243;);<br />
#endif
</p></blockquote>
<p>The <em>#eif</em> directive is a short form for &#8220;#else #if #endif&#8221;, it works the same but you can save some space as you do not need to end with another #endif. If your project has a more complicated release schedule with alpha, beta and production releases you could try this:</p>
<blockquote><p>
#if (!RELEASE)<br />
           Console.WriteLine(&#8221;This is not a release version&#8221;);<br />
    #if (BETA)<br />
          Console.WriteLine(&#8221;Beta, for limited release only&#8221;);<br />
    #eif (ALPHA)<br />
            Console.WriteLine(&#8221;Alpha, for internal testing only&#8221;);<br />
    #endif<br />
#else<br />
           Console.WriteLine(&#8221;Welcome to Widgets 1.0&#8243;);<br />
#endif
</p></blockquote>
<p>As can be seen you can apply several operators to the symbols: ! (not),== (equality), != (inequality), &#038;&#038; (and) and || (or).</p>
<p><a name="D"></a><strong>Avoid the clutter &#8211; use conditional attributes instead</strong></p>
<p>Even a small program has many potential spots where you might like to introduce a debug statement or a log function. If you have to put each of them into a seperate #if DEBUG / #endif block they will start to take up a lot of space in your code. C# has its own slightly more elegant solution to this problem: conditional attributes. They are included in the System.Diagnostics namespace.</p>
<p>In the following program the &#8220;LogLine&#8221; function will only ever run if the &#8220;DEBUG&#8221; symbol has been set.</p>
<pre class="brush: c#">
using System;
using System.Diagnostics;

namespace MyNameSpace
{
    class MainClass
    {
        [ Conditional(&quot;DEBUG&quot;) ]
        public static void LogLine(string msg,string detail)
        {
            Console.WriteLine(&quot;Log: {0} = {1}&quot;,msg,detail);
        }

        public static void Main(string[] args)
        {
            int Total = 0;
            for(int Lp = 1; Lp &lt; 10; Lp++)
            {
                LogLine(&quot;Total&quot;,Total.ToString());
                Total = Total + Lp;
            }
        }
    }
}
</pre>
<p>You can also define combinations chained together with OR to decide if the code is enabled:</p>
<blockquote><p>
        [ Conditional("ALPHA"),Conditional("BETA") ]<br />
        public static void LogLine(string msg,string detail)
</p></blockquote>
<p>Conditionals work differently from the #if/#endif directives that in the above example the &#8220;LogLine&#8221; code will still be included in the assembly. It will never get called however. The compiler will ensure that the LogLine method isn&#8217;t called. In fact if you use a dissasembler (like monodis) the code for Main will look like this:</p>
<pre class="brush: c#">
        public static void Main(string[] args)
        {
            int Total = 0;
            for(int Lp = 1; Lp &lt; 10; Lp++)
                Total = Total + Lp;
        }
</pre>
<p><a name="E"></a><strong>Using #error and #warning during compilation</strong></p>
<p>C# provides the #error and #warning directives to output messages during compilation. As you can expect, if the compiler encounters an #error it will report the problem and stop compiling. The #warning directive just causes a log entry to be displayed in the build output.</p>
<blockquote><p>
#if !DEBUG<br />
    #warning This code is NOT production ready<br />
#endif
</p></blockquote>
<p><a name="F"></a><strong>Generated code builds and error reporting with #line</strong></p>
<p>In regular coding there are not many uses for #line. This directive is mostly useful when you build a code generator that turns one source file into another. Microsoft uses this for example for ASP.NET. Normally if something breaks during such an intermediate step, the compiler would report the error as being in the intermediate file.</p>
<p>The #line directive can force the compiler to report the error as coming from a different line numer, or even a completely different source file.</p>
<pre class="brush: c#">
        public static void Main(string[] args)
        {

#line 250
#if DEBUG
#error This code is NOT production ready
#endif
</pre>
<p>Even though in my original test code the warning was given at line 19 in the source file, the #line directive forces the compiler to report 250 instead.</p>
<p><a name="G"></a><strong>Region Directives</strong></p>
<p>The Visual Studio suite, and the newer MonoDevelop versions allow for outlining of code blocks. You click the little (+) next to the code and it expands the class or method. The #region and #endregion directives allow you to define your own outlined blocks.</p>
<pre class="brush: c#">
#region myregion
        public static void Main(string[] args)
        {
            int Total = 0;
            for(int Lp = 1; Lp &lt; 10; Lp++)
{
                LogLine(&quot;Total&quot;,Total.ToString());
                Total = Total + Lp;
            }
        }
#endregion
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/csharp-preprocessor-directives-explained/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Anonymous types: the Basics</title>
		<link>http://www.dijksterhuis.org/csharp-anonymous-types-basics/</link>
		<comments>http://www.dijksterhuis.org/csharp-anonymous-types-basics/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 12:26:14 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[anonymous]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=617</guid>
		<description><![CDATA[With the introduction of .NET 3.5 C# includes the &#8220;var&#8221; keyword to support anonymous types. One important motivation for this was to make code written with LINQ (Language-Integrated Query) easier to read. So what is an anonymous type? Anonymous types simply mean that you don&#8217;t specify the type &#8212; but let the compiler do it [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><em>With the introduction of .NET 3.5 C# includes the &#8220;var&#8221; keyword to support anonymous types. One important motivation for this was to make code written with LINQ (Language-Integrated Query) easier to read. So what is an anonymous type? Anonymous types simply mean that you don&#8217;t specify the type &#8212; but let the compiler do it instead.</em></p>
<p><span id="more-617"></span></p>
<p>To understand how LINQ works, and to realize that its more than just syntactic sugar added to C# we first need to review a couple of basic concepts that when combined lead to LINQ.</p>
<p><strong>Simple definitions, simplified</strong></p>
<p>In typical C# you will always carefully spell out your definitions:</p>
<blockquote><p>string MyString = &#8220;Hello World&#8221;;</p></blockquote>
<p>From the right side of the declaration it is obvious that only one type (string) will ever match this definition. So instead of us doing the work, why not let the compiler figure this out?</p>
<blockquote><p>var MyString = &#8220;Hello World&#8221;;</p></blockquote>
<p>The above definition will also create a string variable named &#8220;MyString&#8221;. It is important to note that C# is still strongly typed &#8212; unlike scripted languages such as Visual Basic (or PHP) once you have assigned the variable type it sticks. The following will not work:</p>
<blockquote><p>var MyString2 = &#8220;Hello World&#8221;;<br />
MyString2 = 123; // Nice try, but no banana
</p></blockquote>
<p>The compiler will throw an implicit conversion error as 123 cannot be assigned to a string.</p>
<p>The above was an impressive (if somewhat pointless) example of what an anonymous type is. For simple types such as strings, integers etc anonymous types offer little benefits. It is even possible to argue that it reduces your code readability.</p>
<p><strong>Using anonymous types for creating arrays</strong></p>
<p>We are not limited to simple types – the following example show how we can create an anonymous array of integers:</p>
<blockquote><p>int[] myIntArray = new int[] { 1 , 2 , 3 , 4 , 5 , 6 };<br />
var myIntArrayVar = new [] { 1 , 2 , 3 , 4 , 5 , 6 };
</p></blockquote>
<p>The above two examples are equivalent. You still need to specify that you want to create an array using <em>new</em>. You are however limited by how flexible the compiler is. A mixed array is a little too much and we need to force it down to an object array:</p>
<blockquote><p>var myMixedArray = new [] { 1, &#8220;two&#8221; , 3 , &#8220;four&#8221; , 5 };  // this does NOT work<br />
object[] myMixedArray = new object[] { 1, &#8220;two&#8221; , 3 , &#8220;four&#8221; , 5 }; // this is OK
</p></blockquote>
<p><strong>Saving time and code with anonymous types</strong></p>
<p>One very powerful feature of  anonymous types it that they can save you a lot of work. Instead of having to type out everything explicitly, you can have the compiler infer the definition of a complete class:</p>
<pre class="brush: c#">
using System;
class MainClass
{
        public static void Main(string[] args)
        {
            var Person = new { Name = &quot;Michael&quot;, Age = 23 };
            Console.WriteLine(&quot;Name: {0} Age: {1}&quot;,Person.Name,Person.Age);
        }
}
</pre>
<p>The compiler creates an anonymous definition of a class and assigns to it Name and Age as public fields. We can combine anonymous classes and anonymous arrays to create an Employee data structure. Note that we use a &#8220;Var&#8221; in the ForEach loop. Because we are combining anonymous types it is impossible to determine the correct type to loop over each element in the array. Thankfully we can ask the compiler to fill in the correct iterator type for us:</p>
<pre class="brush: c#">
    using System;
    class MainClass
    {
        public static void Main(string[] args)
        {
            var Person1 = new { Name = &quot;Michael&quot;, Age = 23 };
            var Person2 = new { Name = &quot;Sandra&quot;, Age = 33 };
            var Employees = new [] { Person1, Person2 };

            foreach (var P in Employees)
                Console.WriteLine(&quot;Name: {0} Age: {1}&quot;,P.Name,P.Age);

        }
    }
</pre>
<p>As Employees is an array we can use an iterator to step through it as shown above, but we can just as easily use a for loop:</p>
<pre class="brush: c#">
    using System;
    class MainClass
    {
        public static void Main(string[] args)
        {
            var Person1 = new { Name = &quot;Michael&quot;, Age = 23 };
            var Person2 = new { Name = &quot;Sandra&quot;, Age = 33 };
            var Employees = new [] { Person1, Person2 };

            for(int Lp = 0; Lp &lt; Employees.Length; Lp++)
              Console.WriteLine(&quot;Name: {0} Age: {1}&quot;,Employees[Lp].Name,Employees[Lp].Age);
        }
    }
</pre>
<p><strong>Lambda expressions and anonymous methods</strong></p>
<p>Lambda expressions are an extension of Anonymous Methods which were introduced in C# 2.0  Lambda expressions add a lot of power to C#, but they can be hard to understand. They are however great for small filters than can be passed on to functions as variables as we will see in the following example:</p>
<blockquote><p>Func&lt;int,bool&gt; mySeniorStaffFilter = a => a > 35;<br />
Console.WriteLine(&#8221;36 is senior? {0}&#8221;,mySeniorStaffFilter(36));
</p></blockquote>
<p>The above function can be defined anywhere inside your code. It takes a single integer as a parameter and returns a boolean. The integer is defined as &#8220;a&#8221;, and the function checks whether a is larger than 35.</p>
<p>Using the &#8220;old&#8221; anonymous methods we could have written the above as:</p>
<blockquote><p>Func&lt;int,bool&gt; mySeniorStaffFilter = delegate(int a) { return a > 35; };</p></blockquote>
<p>The Lambda expression syntax is the same, but shorter and this make them slightly easier to understand.</p>
<p><strong>Combining Lambda expressions with Queries</strong></p>
<p>If you have used Intellisense in Visual Studio or the greatly improved object browser in MonoDevelop 2.0 you will discover that with C# 3.0 arrays have a much larger set of methods associated with them. One of these methods is &#8220;Where&#8221;. It takes a filter as an argument ; and then simply applies each member of the array to the filter. If the filter returns &#8220;true&#8221; the value is yielded and returned in a sub-set.</p>
<p>In plain language: If you have an array of employees you can pass mySeniorStaffFilter function to the Where method. It checks every staff member to see if their age is over 35 and then returns an array containing only those members of staff. </p>
<blockquote><p>var SeniorStaff = Employees.Where(s => s.Age > 35);</p></blockquote>
<p>The following code example shows how this works:</p>
<pre class="brush: c#">
   using System;
   using System.Linq;

    class MainClass
    {
        public static void Main(string[] args)
        {
            var Employees = new [] { new { Name = &quot;Michael&quot;, Age = 23 },
                                     new { Name = &quot;Anton&quot;, Age = 43 },
                                     new { Name = &quot;Nadia&quot;, Age = 36 },
                                     new { Name = &quot;Lydia&quot;, Age = 34 } };

            var SeniorStaff = Employees.Where(s =&gt; s.Age &gt; 35);          

            foreach(var Member in SeniorStaff)
             Console.WriteLine(&quot;Name: {0} Age: {1}&quot;,Member.Name,Member.Age);

        }
    }
</pre>
<p>From the above you can see that the compiler needs to infers,create and apply several anonymous types to create a working code sample. We thankfully can use &#8220;var&#8221; to specify our SeniorStaff sub-set. Behind the scenes the compiler has created an anonymous IEnumberable for SeniorStaff that contains our Employee records with anonymous staff members. And for the Where method it applied an anonymous delegate to create the correct filter. </p>
<p>And this leads us to the first real &#8220;LINQ&#8221; statement.</p>
<blockquote><p>var SeniorStaff = from S in Employees where S.Age > 35 select S;</p></blockquote>
<p>When you take away the syntactic sugar the C# compiler will build from this the same (or very similar) &#8220;Employees.Where(s => s.Age > 35)&#8221; statement. Using the same anonymous methods, variables and functions as we used earlier.</p>
<p>This took a lot of getting to. It is however important to understand that LINQ is implemented in C# through basic, strongly typed variables and methods.</p>
<p>To keep the complexity of our C# code down the &#8220;var&#8221; keyword was added allowing the programmer to focus on coding the logic of the program instead of having to write long hand type definitions for each variable. </p>
<p><strong>Q. Is var typesafe?</strong></p>
<p>There&#8217;s no concern about type-safety, as the variable created is not dynamic. It&#8217;s just compiler magic and any type unsafe calls you make will get caught.</p>
<p><strong>Q. Is there a performance penalty for using &#8220;var&#8221; ?</strong></p>
<p>The var keyword only tells the compiler to infer the type from the assignment, there&#8217;s no runtime difference, so no, there&#8217;s no penalty performance wise.</p>
<p><strong>Q. On using anonymous definitions in large projects</strong></p>
<p>Anonymous types cannot be shared across assembly boundaries. The compiler ensures that there is at most one anonymous type for a given sequence of property name/type pairs within each assembly. To pass structures between assemblies you will need to properly define them.</p>
<p><strong>References</strong></p>
<p>   1. <a href="http://msdn.microsoft.com/en-us/library/bb308959.aspx">http://msdn.microsoft.com/en-us/library/bb308959.aspx</a></p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/csharp-anonymous-types-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Hello World for LINQ on Mono</title>
		<link>http://www.dijksterhuis.org/csharp-hello-world-linq-mono/</link>
		<comments>http://www.dijksterhuis.org/csharp-hello-world-linq-mono/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 09:59:19 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=612</guid>
		<description><![CDATA[If you are up to date with your Ubuntu distribution you will be using Mono 1.9.1 which has support for C# LINQ build in. Enabling it is as easy as adding &#8220;using System.Linq&#8221; to your project.  The following code snippet is the LINQ equivalent of a &#8220;Hello World&#8221; application:      [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p>If you are up to date with your Ubuntu distribution you will be using Mono 1.9.1 which has support for C# LINQ build in. Enabling it is as easy as adding &#8220;using System.Linq&#8221; to your project.  The following code snippet is the LINQ equivalent of a &#8220;Hello World&#8221; application:       </p>
<pre class="brush: c#">
    using System;
    using System.Linq;

    class MainClass
    {

        public static void Main(string[] args)
        {
            String[] world = { &quot;Hello World&quot;, &quot;Hello Mars&quot; , &quot;Hello Venus&quot; };

            var rightOne = from s in world where s.EndsWith(&quot;World&quot;) select s;

            foreach(string planet in rightOne)
                Console.WriteLine(&quot;{0}&quot;,planet);
        }
    }
</pre>
<p>The first time I tried compiling it,  MonoDevelop gave me the following error:</p>
<blockquote><p>An implementation of `Where&#8217; query expression pattern could not be found. Are you missing `System.Linq&#8217; using directive or `System.Core.dll&#8217; assembly reference?(CS1935)]
</p></blockquote>
<p>You can solve this by ensuring that you have added &#8220;System.Core&#8221; as a reference to your solution/project. You can do this by right clicking &#8220;References&#8221; in the &#8220;Solution Panel&#8221; and selecting &#8220;Edit References&#8221;.</p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/csharp-hello-world-linq-mono/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
