Please excuse the short, direct sentence structure used in the documentation. This style is more easily translated into other languages and I want to make it easy to understand the project documentation.

Alternate Language Documentation:

Topics:

 

What is MicroLinq?

MicroLinq is a reduced-size reimplementation of LINQ for the .NET Micro Framework (NETMF). MicroLinq was developed based on the NETMF 4.1 release available from Microsoft. It was developed on a Netduino Plus development board from Secret Labs with Visual Studio 2010.

 

The .NET Micro Framework itself is a reduced implementation of the full .NET Framework. Because of this, many features developers have come to rely on are not available. In my daily work I often use LINQ in place of a lot of other code. The statements are easy to read which makes the code self-documenting. Because the extensions reduce the amount of code that has to be written, programs are much easier to understand.


Some limitations of NETMF to consider:

  • NETMF does not support generics. This is why the examples use casting so heavily. Everything is an object.
    • Visual Studio will not prevent you from using generics in your code but the project will not compile.
  • In NETMF 4.1 System.String implements the required methods of IComparable but isn't marked as such.
    • I have worked around this limitation where needed but it is less efficient than it could be.
    • This is corrected by the NETMF for version 4.2.
  • The NearlyGenericArrayList contains a sort method. It is probably unnecessary. I thought it would be faster than the OrderBy extension.

 

Using MicroLinq is very similar to LINQ except without support for generics the code must be specifically type casted. For instance:

LINQ:

var MyCollection = Utility.GetEntries();

var Results = MyCollection.Where(item => item.Id < 10);

 

MicroLinq:

var MyCollection = Utility.GetEntries();

var Results = MyCollection.Where(item => ((MyItemType)item).Id < 10);

 

Q: Why can’t the type be inferred?

A: The MicroLinq methods are just extension methods. They work just like any other method. The signature of the method shows what is happening.

public static IEnumerable Where(this IEnumerable e, Predicate p);

 

In the .NET Framework Predicate delegate is a generic type.

public delegate bool Predicate<in T>(T obj);

 

Generic types aren’t available in the .NET Micro Framework. The Predicate delegate does not even exist until introduced by MicroLinq.

public delegate bool Predicate(object o);

 

Without the type information provided by the generic signature, the delegate must be told about the parameter’s type. To be as universal as possible, the Predicate delegate defines the parameter as an object. Everything in the .NET Framework is an object so any type can be used.

 

The predicate is generally applied during an iteration of an IEnumerable. Due to the same restriction on generics, IEnumerable is also just an enumerable set of objects. If there were additional delegates with specific type parameters there would also need to be matching IEnumerable classes. The typed IEnumerable classes could inherit from the base IEnumerable but delegates cannot. Each extension method would have to be overloaded to accept all the specific typed delegates.

I chose not to do this for a few reasons:

  1. Maintainability of the source would be harder.
  2. It would take more effort to guess which methods would be most useful and implement them than it would to just cast the objects.
  3. NETMF is designed for resource limited environments. The goal at all times is to have a small footprint.

Last edited Feb 18, 2011 at 7:29 PM by VikingErik, version 18

Comments

No comments yet.