CodeProject, Lambdas / Anonomous delegates

Func’ ing hell

Today I was looking at an article over at codeproject which used some lambda expression in conjunction with some of the inbuilt .NET delegates such as

Func<T,TResult> and

Func<TResult>

And this started me thinking, that all very well using these convenience delegates, but what if I want more parameters etc etc.

So I started to look into this a bit more. If we look at the object browser in VS2008 we can see that the following FUNC delegates exist

func.png

And if we look further at this in VS2008 by looking at one of these FUNC types, say FUNC<T,TResult> in VS2008 we can see the following

func2.png

So Func<T,TResult> is just a short hand for a delegate declaration. Mmm. This means that we can create our own.

So lets have a look at that. Here is a screen shot of a simple console application where I’ve created my own delegate type.

func3.png

And running this produces the following result

func4.png

The important things to note here are as follows

  • => is a lambda which is equivalent to an anonomous delegate being declared
  • () => means that the lambda expression takes no input params
  • (x) => means the lambda expression takes a parameter named x
  • Func<bool> means the function takes 0 params but returns a bool. This maps on to the existing System.Func<TResult> type
  • Func<int,bool> means the function takes an int param and returns a bool. This maps on to the existing System.Func<T,TResult> type

And we can create our own like

public delegate int SimpleReturn<T>(T input);

which we can use as follows

mySimpleReturn(20);

….

public static SimpleReturn<int> mySimpleReturn = (x) =>{

Console.WriteLine(“mySimpleReturn “ + x + ” * 2 “ + ” = “ + (x * 2).ToString());

return x * 2;};

CodeProject, LINQ

Writing Custom LINQ Extension Methods

Microsoft have done a great job with LINQ IMHO. However there are times when it might be handy to create your own LINQ extension methods. LINQ extension methods can be applied to any type provided that the source is of type IEnumerable<T>, so thats really the only requirement.

So how do we write an extension method. Well its quite simple. Here are 2 that I’ve written for a  IEnumerable<string> the first example takes a Predicate and the second one doesnt. This extension method will check whether a file name string is a valid image file, but you can do it for whatever your pupose is.

customlinq.png

The only other thing to note is the cryptic “this” keyword used in the method signature. This means its being applied to the current IEnumerable<T> collection. Just always put it in your extension methods and you’ll be fine.

So how to use these extension methods, well providing your dealing with a  Enumerable<string> collection youll get the option coming up as an extension method.

vs2008linq.png

And to actually use it you could use a standard LINQ query something like

usingcustomlinq.png

CodeProject, LINQ

How to stream XLINQ

From time to time when you are using XLINQ to query XML data, you may find that you are dealing with a very large XML file. Loading a very large XML file using the standard XLINQ Load() method may not be appropraite due to the size of the XML file. Instead some sort of streaming XML approach may be prefferable.

But XLINQ doesnt offer this facility straight out of the box, but with a bit of trickery we can get a XMLReader to do this streaming for us. Lets see an example shall we.

stream12.png

Thats the standard XLINQ query, but we still need to use an XmlReader to do the streaming, so lets see that part, its simply as shown below

stream22.png

And thats it, its a simple as that. We can now query a large XML file using XLINQ and be safe in the knowledge that it will be streamed using an XmlReader and the Yield keyword.