Extension Methods

There are 7 entries for the tag Extension Methods
In response to a comment on one of my previous posts (Safely Overriding Equals), I've decided to post some comments about Extension methods in general. A friend asked me what I gained out of extending string to convert to an enum. He asked, "is it just 'syntactic sugar'?" The simple answer is, "Yes." In fact, this is true of many extensions. Clearly, the major advantage of extensions is changing the syntax of how they are called. In fact, I have written a few extensions that support chaining and ......
It seems that enums might be the neglected step-child when it comes to Generics and Extensions. I've tried defining generics that are constrained to enums. No luck. I faced much frustration trying to get my generic methods to work. I finally came up with a method that meets my need. But, I had to add a little bit of run-time type checking. Instead of repeating Enum.Parse throughout my code, I wanted to create a simple generic extension method that would do it for me. I just discovered that my extension ......
So, I've settled on a pattern that I found somewhere on the web to safely override Equals. I don't remember where I found it, and there are certainly many other good implementations (probably better than mine). Regardless, I found that I do have a repeating pattern. I decided to set out to limit this duplication if I could. Here is what I came up with. 1: public static bool SafeEquals<T>( this T left, object right, Predicate<T> test ) 2: { 3: if( right != null && right.GetType().Equals( ......
The string.Join method can come in handy when you want a comma separated list of strings. However, there's a major limitation. To use it, you must provide a one-dimensional string array. What if you have a collection of objects and you want to "Join" a property on the objects (e.g., a comma separated list of IDs). Well, since I couldn't find anything within the framework that would do this for me, I wrote an extension method that meets my need. Updated from James Curran's suggestion in the comments. ......
I cannot believe that I am the first person ever to encounter the following error: MSB3095: Invalid argument. Illegal characters in path. [Update] As Tom pointed out in the comments, this issue is directly related to the encoding of my *.refresh file. Thanks Tom. I searched Google and other search engines with no success. I found "MSB3095: Invalid Argument", and I found "Illegal characters in path.", but never in the same place. I did not actually resolve the issue; I was able to work around it. ......
C# 3.0 has certainly introduced some really cool features. I have used the Automatic Properties extensively as well as object and collection initializers. These are real time savers. However, the most exciting feature (IMHO) are Extension Methods. My last post shows one example of how powerful extension methods can be. Here is another example (inspired by Scott Gu). 1: public static class Extensions 2: { 3: /// 4: /// Do not use this extension for large sets because it iterates through 5: /// the ......
How many times have you written something like this? 1: public string MyStringProperty 2: { 3: get 4: { 5: object s = ViewState["MyStringProperty"]; 6: if( s == null ) 7: { 8: s = "MyDefaultValue"; 9: } 10: return (String)s; 11: } 12: set { ViewState["MyStringProperty"] = value; } 13: } 14: 15: public bool MyBooleanProperty 16: { 17: get 18: { 19: object b = ViewState["MyBooleanPropert... 20: if( b == null ) 21: { 22: b = true; 23: } 24: return (bool)b; 25: } 26: set { ViewState["MyBooleanProperty"] ......