Home About

 February 10th, 2009

C# Anonymous types: the Basics - 0

With the introduction of .NET 3.5 C# includes the “var” 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’t specify the type — but let the compiler do it instead.

Read the rest of this entry »

 February 9th, 2009

C# Hello World for LINQ on Mono - 1

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 “using System.Linq” to your project. The following code snippet is the LINQ equivalent of a “Hello World” application:

    using System;
    using System.Linq;

    class MainClass
    {

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

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

            foreach(string planet in rightOne)
                Console.WriteLine("{0}",planet);
        }
    }

The first time I tried compiling it, MonoDevelop gave me the following error:

An implementation of `Where’ query expression pattern could not be found. Are you missing `System.Linq’ using directive or `System.Core.dll’ assembly reference?(CS1935)]

You can solve this by ensuring that you have added “System.Core” as a reference to your solution/project. You can do this by right clicking “References” in the “Solution Panel” and selecting “Edit References”.


Recent Comments
  • Ales: Hi, Thanks for the code… I must say I did not experience any errors decrypting any of my messages. I even...
  • JC: Thanks very useful and well explained
  • Thomas: This is a public static class written in the C# language that does not save state. You can call into the...
  • Simon: Thank you very much for this post! It helped me essentially to overcome obstacels to work with mono!
  • Graham: This is a good research for keyboard shortcuts! Some shortcuts are also compatible for Windows OS. I have...