Int32.TryParse(…)
April 17, 2005
Amen!
Predicate<T> and Generic Collections
April 17, 2005
If you are a .NET programmer and trying to stay current you are probably looking at the current BETA (2) of Visual Studio 2005 and wondering whats new. One of the big changes in .NET 2.0 is the introduction of generics into the runtime and the inclusion of a number of stock standard generic types into the base class library.
I’m not going to go into the whole generic type system in this post since there has been heaps written about the implementation details already, but I did want to look at some of the new methods that we get on the generic collection classes and how they use generics and anonymous methods to allow C# programmers to write much more expressive code.
Find(…) and FindAll(…) Methods and the Predicate<T> Delegate
If you take a look at the List<T> type in System.Collections.Generic namespace you will see that it has a Find and FindAll method. Both of these methods take a Predicate<T> which is a generic delegate type. What the hell is a “predicate”?
Well – it depends on who you ask. Most English majors would probably give you a definition which would have you scurrying off to the dictionary to try and understand what they just said, however, if you ask a programmer then they would probably tell you something like this.
A predicate is a condition against which a piece of data is evaluated against to determine whether that condition is true or false. Basically, its a piece of code that returns true or false depending on the input.
So lets apply this ground breaking knowledge to the purpose of finding the elements of a collection. By passing as Predicate<T> to the Find or FindAll methods we are asking the collection to return us object(s) that match the condition specified by the code that the predicate points to:
Thats pretty straight forward, but what gets interesting is when you mix generic predicates with anonymous methods, a C# specific feature that happened to be the subject of the first post this blog over twelve months ago. With anonymous methods, the above code could be expressed as:
I’ll leave it to you to decide for yourself which implementation is more elegant. Back when I posted the first article to this blog I suggested that users of anonymous methods evaluate their readability, and I think that still holds true – I wouldn’t make a global ruling.
Just for kicks, consider this implementation – how would you do it without anonymous methods?
You can download the demo code from here.
Modified Expansion: mdctor
April 17, 2005
One of the expansions that I thought could use a little spice was the “ctor” expansion. You can download my modified one here. Its not very different, all it does is insert a default “string foo” argument that you can overrite in the template. I found that in every instance I wanted to use the “ctor” expansion I also wanted to put in some constructor arguments – especially for entity classes.
Enjoy!
Hardware Upgrades
April 17, 2005
Chris Hewitt is thinking about the next round of hardware upgrades he is considering. Its something that has been on my mind too. I really want a Tablet PC because the interface really appeals to me for a number of things that I like to do (diagramming, taking notes, drawing), but at the moment the screen resolutions are just so small, my two year old Dell is an Inspiron 8500 and in many respects it still out performs current machines.
If I wanted to extend the life of my machine over the next six to twelve months I would have the following on my shopping list:
- 2GB RAM (I currently have 1GB).
- A high speed hard disk.
- A new battery.
When is Dell going to produce a Tablet PC (full stop) for power users like me?
P.S. The Nemo reference is probably something to do with an internal intranet page with have that keeps track of where people are – since we all travel a bit.
Int32.TryParse(…)
April 17, 2005
Amen!
Predicate<T> and Generic Collections
April 17, 2005
If you are a .NET programmer and trying to stay current you are probably looking at the current BETA (2) of Visual Studio 2005 and wondering whats new. One of the big changes in .NET 2.0 is the introduction of generics into the runtime and the inclusion of a number of stock standard generic types into the base class library.
I’m not going to go into the whole generic type system in this post since there has been heaps written about the implementation details already, but I did want to look at some of the new methods that we get on the generic collection classes and how they use generics and anonymous methods to allow C# programmers to write much more expressive code.
Find(…) and FindAll(…) Methods and the Predicate<T> Delegate
If you take a look at the List<T> type in System.Collections.Generic namespace you will see that it has a Find and FindAll method. Both of these methods take a Predicate<T> which is a generic delegate type. What the hell is a “predicate”?
Well – it depends on who you ask. Most English majors would probably give you a definition which would have you scurrying off to the dictionary to try and understand what they just said, however, if you ask a programmer then they would probably tell you something like this.
A predicate is a condition against which a piece of data is evaluated against to determine whether that condition is true or false. Basically, its a piece of code that returns true or false depending on the input.
So lets apply this ground breaking knowledge to the purpose of finding the elements of a collection. By passing as Predicate<T> to the Find or FindAll methods we are asking the collection to return us object(s) that match the condition specified by the code that the predicate points to:
Thats pretty straight forward, but what gets interesting is when you mix generic predicates with anonymous methods, a C# specific feature that happened to be the subject of the first post this blog over twelve months ago. With anonymous methods, the above code could be expressed as:
I’ll leave it to you to decide for yourself which implementation is more elegant. Back when I posted the first article to this blog I suggested that users of anonymous methods evaluate their readability, and I think that still holds true – I wouldn’t make a global ruling.
Just for kicks, consider this implementation – how would you do it without anonymous methods?
You can download the demo code from here.
Modified Expansion: mdctor
April 17, 2005
One of the expansions that I thought could use a little spice was the “ctor” expansion. You can download my modified one here. Its not very different, all it does is insert a default “string foo” argument that you can overrite in the template. I found that in every instance I wanted to use the “ctor” expansion I also wanted to put in some constructor arguments – especially for entity classes.
Enjoy!
Hardware Upgrades
April 17, 2005
Chris Hewitt is thinking about the next round of hardware upgrades he is considering. Its something that has been on my mind too. I really want a Tablet PC because the interface really appeals to me for a number of things that I like to do (diagramming, taking notes, drawing), but at the moment the screen resolutions are just so small, my two year old Dell is an Inspiron 8500 and in many respects it still out performs current machines.
If I wanted to extend the life of my machine over the next six to twelve months I would have the following on my shopping list:
- 2GB RAM (I currently have 1GB).
- A high speed hard disk.
- A new battery.
When is Dell going to produce a Tablet PC (full stop) for power users like me?
P.S. The Nemo reference is probably something to do with an internal intranet page with have that keeps track of where people are – since we all travel a bit.
Comparison<T> and Collection Sorting
April 17, 2005
After looking at Predicate<T> I thought it would be cool to take a squizz at the Comparison<T> generic delegate type and the Sort(…) method found on generic collections. Comparison<T> allows you to sort lists of items in the same way that you used to use an IComparer, the difference is that you can declare them inline with your method which means you don’t have proliferation of type definitions in your code (**).
Look at the first commented section, here I am simply sorting by the driver’s rating. In the second commented section I am doing a slightly more complex sort by sorting by rating AND THEN by the driver’s first name. For simple sorts you’d probably do it inline with the method call versus splitting it out into its own block, but for more complex sorting I would recommend defining it and stuffing the implementation into its own couple of lines of code.
You can download the code for this demo here.
** Note that while you don’t end up creating additional types in your code, the C# compiler actually produces a method and field for each anonymous method. This is a little bit different to what I observed with earlier versions of VS2005 which produced a whole extra type.
Kim’s Second Blog Post
April 17, 2005
Kim, our communications manager, has posted up her second blog post – this one is on the importance of corporate messaging. I know Kim sent around an e-mail internally a little while back about getting our views on some of strengths of the courseware we deliver to students. How much of that kind of stuff goes into building the messaging?