James Michael Hare

...hare-brained ideas from the realm of software development...
posts - 137 , comments - 1106 , trackbacks - 0

My Links

News

Welcome to my blog! I'm a Sr. Software Development Engineer in Seattle, WA. I've been doing C++/C#/Java development for over 18 years, but have definitely learned that there is always more to learn!

All thoughts and opinions expressed in my blog and my comments are my own and do not represent the thoughts of my employer.

Blogs I Read

MCC Logo MVP Logo

Follow BlkRabbitCoder on Twitter

Tag Cloud

Archives

Post Categories

Extension Methods

There are 26 entries for the tag Extension Methods
C#/.NET Little Wonders: Extension Methods Demystified
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found here. I have had the pleasure to program in a variety of programming languages throughout the years including the trifecta of C++, Java, and C#. It's often interesting how these three languages are so similar and yet have such key differences as well. Each of them has ......

Posted On Friday, March 8, 2013 12:47 AM | Comments (6) |

C#/.NET Little Wonders: Skip() and Take()
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found here. I’ve covered many valuable methods from System.Linq class library before, so you already know it’s packed with extension-method goodness. Today I’d like to cover two small families I’ve neglected to mention before: Skip() and Take(). While these methods seem so ......

Posted On Thursday, March 29, 2012 5:58 PM | Comments (3) |

C#/.NET Little Wonders–The List<T> Range Methods
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found here. LINQ has added so many goodies that can be used to query IEnumerable<T> sequences that it can be easy to lose sight of some of the methods that are unique to each of the collection classes. This week, we will look at the range series of methods in the List<T> ......

Posted On Thursday, February 23, 2012 6:01 PM | Comments (5) |

C#/.NET Little Wonders: The SequenceEqual() Method
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found here. First of all, thanks for all the well-wishers for me and my family, things are starting to settle down a bit, so I hope to be able to continue to blog at least on a bi-weekly basis – and I’m hoping to sprinkle in some C++ blog entries as well. But for today, we’re ......

Posted On Thursday, February 16, 2012 7:14 PM | Comments (8) |

C#/.NET Little Wonders: The Predicate, Comparison, and Converter Generic Delegates
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found here. In the last three weeks, we examined the Action family of delegates (and delegates in general), the Func family of delegates, and the EventHandler family of delegates and how they can be used to support generic, reusable algorithms and classes. This week I will ......

Posted On Thursday, November 24, 2011 9:03 PM | Comments (3) |

C#/.NET Little Wonders: Searching Strings With Contains(), StartsWith(), EndsWith(), and IndexOf()
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders post can be found here. Two weeks ago I decided to stop my Little Wonders in the String class, but I recanted and decided to do one more before wrapping up String. So today we’ll look at ways to find a out if a given source String has a target String inside of it (and where). IndexOf() ......

Posted On Thursday, October 13, 2011 6:23 PM | Comments (6) |

C#/.NET Toolbox: Creating a "Safer" Task Dispose()
This post is another in a series that contains generic utility classes I’ve developed along the way to help make coding a bit easier. If these already exist as part of the framework and I’ve overlooked them, feel free to let me know! And if you know of a better way to implement them, do the same! I’m never too old to learn something new (I hope!). Update: modified the TryDispose() method to check for IsCompleted first and mark any Task exceptions as handled. So recently, I’ve been moving some older ......

Posted On Thursday, October 6, 2011 7:41 PM | Comments (2) |

C#/.NET Little Wonders: String Padding and Trimming - Not Just for Spaces!
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders post can be found here. This post continues a series of Little Wonders in the BCL String class. Yes, we all work with strings in .NET daily, so perhaps you already know most of these. However, there are a lot of little fun things that the String class can do that often get overlooked. ......

Posted On Thursday, September 22, 2011 6:27 PM | Comments (7) |

C#/.NET Little Wonders: Five Easy Sequence Aggregators
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders post can be found here. Today we will look at five easy ways to aggregate sequences. Often times when we’re looking at a sequence of objects, we want to do perform some sort of aggregation across those sequences to find a calculated result from the sequence. The methods we will be looking ......

Posted On Thursday, August 25, 2011 9:05 PM | Comments (5) |

C#/.NET Little Wonders: Empty(), DefaultIfEmpty(), and Count() Extension Methods
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders post can be found here. On this post I will finish examining the System.Linq methods in the static class Enumerable by examining two extension methods Count() and DefaultIfEmpty(), and one static method Empty(). The Empty() static method How many times have you had to return an empty collection ......

Posted On Thursday, June 2, 2011 7:08 PM | Comments (24) |

C#/.NET Little Wonders: The LINQ Set Operations -- they're not just for math!
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. Today we are going to examine the LINQ set operations that are part of the IEnumerable<T> extension methods. Now, most of the time when people think of set operations they think of the math or logic classes they are usually taught in, but really these LINQ methods have a much larger appeal and applicability than just math ......

Posted On Thursday, May 5, 2011 7:13 PM | Comments (12) |

C#/.NET Little Wonders: The ElementAt() and Last() Extension Methods
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. Most of my time this week has been spent finishing the current iteration at work and updating a presentation for the Springfield DNUG, so today’s post will be a bit on the lighter side, but I wanted to continue my post series so I thought it would be a good time to quickly mention the ElementAt() and Last() LINQ extension methods. ......

Posted On Thursday, April 28, 2011 7:35 PM | Comments (4) |

C#/.NET Little Wonders: The Any() and All() Extension Methods
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can really help improve your code by making it easier to write and maintain. Today we’re going to look at two more handy LINQ extension methods: the Any() and All() methods. These methods can help you examine an enumerable collection to determine if any, or all, of the items in the collection meet some logical condition. Any() – Checks for at least one match The Any() method is a LINQ extension ......

Posted On Thursday, April 21, 2011 6:55 PM | Comments (18) |

C#/.NET Little Wonders: First() and Single() - Similar Yet Different
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can really help improve your code by making it easier to write and maintain. Today we're going to look at two LINQ extension methods that are both very similar and yet very different. Logically, First() and Single() serve similar purposes, but there is a subtle difference between these two that if you aren't expecting it may give you very different behaviors. First() - Retrieves the first occurrence ......

Posted On Thursday, April 14, 2011 7:09 PM | Comments (16) |

C#/.NET Toolbox: Adding a ToHashSet() Extension Method
This post is another in a series that will just contain little GUCs (Generic Utility Classes) I’ve developed along the way. If these already exist as part of the framework and I’ve overlooked them, feel free to let me know! And if you know of a better way to implement them, do the same! I’m never too old to learn something new (I hope!). I've blogged in the past about how useful the overlooked HashSet class can be (here) and about the very useful ToDictionary(), ToList(), and ToLookup() LINQ extension ......

Posted On Thursday, March 31, 2011 6:21 PM | Comments (1) |

C#/.NET Little Wonders: The ToLookup() LINQ Extension Method
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can really help improve your code by making it easier to write and maintain. LINQ has added so many wonderful extension methods to our .NET toolbox! In particular, there are a few methods that are useful for creating a collection from an IEnumerable<T> instance. The ToDictionary() and ToList() extension methods, for example, are handy for taking an IEnumerable<T> and creating a Dictionary ......

Posted On Thursday, March 24, 2011 5:39 PM | Comments (5) |

C#/.NET Little Wonders: The Useful But Overlooked Sets
Once again we consider some of the lesser known classes and keywords of C#. Today we will be looking at two set implementations in the System.Collections.Generic namespace: HashSet<T> and SortedSet<T>. Even though most people think of sets as mathematical constructs, they are actually very useful classes that can be used to help make your application more performant if used appropriately. For more of the "Little Wonders" posts, see the index here. A Background From Math In mathematical ......

Posted On Thursday, February 3, 2011 6:23 PM | Comments (13) |

C#/.NET Fundamentals: Finding an Item’s Index in IEnumerable<T>
Sorry for the long blogging hiatus. First it was, of course, the holidays hustle and bustle, then my brother and his wife gave birth to their son, so I’ve been away from my blogging for two weeks. Background: Finding an item’s index in List<T> is easy… Many times in our day to day programming activities, we want to find the index of an item in a collection. Now, if we have a List<T> and we’re looking for the item itself this is trivial: 1: // assume have a list of ints: 2: var list = ......

Posted On Thursday, January 6, 2011 7:29 PM | Comments (3) |

C#/.NET Little Wonders: ToDictionary() and ToList()
The Little Wonders series received so much positive response I decided to make it a recurring theme in my blog as new ones popped in my head. There are two simple, yet great, LINQ extension methods you may or may not know about, but that can really simplify the task of converting collection queries into collections: ToDictionary() and ToList(). Introduction: LINQ and Deferred Execution Depending on your knowledge of LINQ, you may be oblivious as to what many of these query expressions do behind the ......

Posted On Thursday, October 21, 2010 6:42 PM | Comments (31) |

C# Toolbox: A Debuggable, Self-Installing Windows Service Template (2 of 2)
Update: I have now placed the zip containing the source for the end result of part 1 and 2 of this service template here. Two weeks ago I began the series with a discussion on how to make a new C# Windows Service template that is “debuggable” (you can find the article here). I had then intended the next week to follow up with a discussion on how to modify that template to make the service “self-installing”. Unfortunately, with my work schedule I wasn’t able to complete the series last week due to ......

Posted On Thursday, October 7, 2010 4:43 PM | Comments (24) |

C# 5.0 and Beyond - My Wish List
I apologize in advance to those waiting for part 2 of the Windows Services post I did last week. I will have a follow-up post next week, this week at work has just been crazy and I haven’t had as much time to devote to the code examples as I’d like to polish it. So instead I thought I’d throw a quick post out on what I’d like to see in the future versions of C#. A lot of folks are posting things they would like to see in C# 5.0 and beyond. There’s a great list on Stack Overflow (here) and a great ......

Posted On Friday, October 1, 2010 10:15 AM | Comments (8) |

C#/.NET Five Final Little Wonders That Make Code Better (3 of 3)
This week is my final entry in my Little Wonders series (posts one and two are here and here). These are those little tips and tricks in .NET (and in particular C#) that may not seem like much, but can help make your code either more concise, maintainable, or performant. Thanks again for all your comments and suggestions on this series, it has been a lot of fun to write, and though I feel I could go on forever with lots more tips and tricks, this will be the last Little Wonders post for a while. ......

Posted On Thursday, September 9, 2010 6:06 PM | Comments (33) |

C#: Adding Functionality to 3rd Party Libraries With Extension Methods
Ever have one of those third party libraries that you love but it's missing that one feature or one piece of syntactical candy that would make it so much more useful? This, I truly think, is one of the best uses of extension methods. I began discussing extension methods in my last post (which you find here) where I expounded upon what I thought were some rules of thumb for using extension methods correctly. As long as you keep in line with those (or similar) rules, they can often be useful for adding ......

Posted On Wednesday, April 28, 2010 10:58 PM | Comments (1) |

C# Extension Methods - To Extend or Not To Extend...
I've been thinking a lot about extension methods lately, and I must admit I both love them and hate them. They are a lot like sugar, they taste so nice and sweet, but they'll rot your teeth if you eat them too much. I can't deny that they are useful and handy. After all, one of the major components of the Shared Component library where I work is a set of useful extension methods to help simplify common repetitive tasks. But, I also can't deny that they tend to be overused and abused to willy-nilly ......

Posted On Monday, April 26, 2010 10:44 PM | Comments (20) |

More Fun with C# Iterators and Generators
In my last post, I talked quite a bit about iterators and how they can be really powerful tools for filtering a collection of items down to a subset of items. This had both pros and cons over returning a full collection, which, in summary, were: Pros: If traversal is only partial, does not have to visit rest of collection. If evaluation method is costly, only incurs that cost on elements visited. Adds little to no garbage collection pressure. Cons: Very slight performance impact if you know caller ......

Posted On Wednesday, April 21, 2010 10:31 PM | Comments (3) |

Of C# Iterators and Performance
Some of you reading this will be wondering, "what is an iterator" and think I'm locked in the world of C++. Nope, I'm talking C# iterators. No, not enumerators, iterators. So, for those of you who do not know what iterators are in C#, I will explain it in summary, and for those of you who know what iterators are but are curious of the performance impacts, I will explore that as well. Iterators have been around for a bit now, and there are still a bunch of people who don't know what they are or what ......

Posted On Monday, April 19, 2010 10:12 PM | Comments (4) |

Powered by: