extension-methods

extension method on type and nullable<type>

For sake of simplicity, let's assume I want to write an extension method for the type int? and int: public static class IntExtentions { public static int AddOne(this int? number) { var dummy = 0; if (number != null) dummy = (int)number; return dummy.AddOne(); } public static int AddO...

Suggestions For String and DateTime utility functions' Library using Extension Methods.

I'm writing a library of Extension Methods for String and DateTime utility functions in C#. Can you please help me out by suggesting the useful utlity functions for String and DateTime you may want to be part of it ? With your suggestions I can make it more cohesive and Collective. Thanks! ...

LINQ-to-SQL and Extension method in subQuery

My extension method is: public static IEnumerable<T> FilterCultureSubQuery<T>(this Table<T> t) where T : class { return t; } I tried to use it in this query var test = from p in t.Products select new { Allo = p, Allo2 = (from pl in t.ProductLocales.FilterCultu...

Virtual Extension Methods?

I have a class that gets used in a client application and in a server application. In the server application, I add some functionality to the class trough extension methods. Works great. Now I want a bit more: My class (B) inherits from another class (A). I'd like to attach a virtual function to A (let's say Execute() ), and then imple...

inner join inside extension method (linq)

I am trying to write an extention method for a repository. The repository has a method IQueryable<parent> GetAll(). I want to create an extention method FilterByChildId(int childId) so that I can write: List<parent> data = from d in repository.getAll().FilterByChildId(33).ToList() Not sure how to do the join on the parent and child i...

UrlHelper extension method call encoded not executed

I created a simple extension method for the ASP.NET MVC UrlHelper. It takes no arguments as its job is to look up the name of a stylesheet file from the configuration and return a url to the stylesheet. The extension method looks roughly like this: public static string SiteStylesheet(this UrlHelper urlHelper) { var scriptFilename = ...

C#: Extension methods not allowed in nested static classes?

Why is this? I would find it really nice to be able to have some extension methods locked down to be used only within one of my classes. Don't really want to have certain extension methods available everywhere... and they look so much nicer than regular static methods :P For clarification: The reason I want these extension methods is...

C# Convert string to nullable type (int, double, etc...)

I am attempting to do some data conversion. Unfortunately, much of the data is in strings, where it should be int's or double, etc... So what I've got is something like: double? amount = Convert.ToDouble(strAmount); The problem with this approach is if strAmount is empty, if it's empty I want it to amount to be null, so when I add i...

Using extension methods defined in C# from F# code

I have a series of extension methods defined for various classes in a C# library. I'm currently writing some F# code and instead of rewriting that code I would simply like to use my existing extension methods in my F# code. I have added a reference to the library and used the open statement to import the namespace, but the extension met...

Using Extension Methods with .NET Framework 2.0

Under Visual Studio 2008 Can I create an Extension Method to work under a .NET Framework 2.0 project? ...

Interface + Extension (mixin) vs Base Class

Is an interface + extension methods (mixin) preferable to an abstract class? If your answer is "it depends", what does it depend upon? I see two possible advantages to the interface + extension approach. Interfaces are multiply inheritable and classes are not. You can use extension methods to extend interfaces in a non-breaking way. ...

What are extension-methods -- and what makes them different from other methods? [Duplicate]

Duplicate What are Extension Methods? Usage of Extension Methods What Advantages of Extension Methods have you found? So I run into the term "extension-methods" frequently, when reading about .Net and intellisensing (!) around... What are extension-methods -- and what sets them apart from other methods? ...

Do extension methods violate software engineering principles?

I tend to shy away from extension methods (in C# ), as it violates 'code cohesion' principle of software engineering. Then, why is it there? or is my understanding wrong? ...

C#: Best practice for validating "this" argument in extension methods

Let's say I have an extension method public static T TakeRandom<T>(this IEnumerable<T> e) { ... To validate the argument e, should I: A) if (e == null) throw new NullReferenceException() B) if (e == null) throw new ArgumentNullException("e") C) Not check e What's the consensus? My first thought is to always validate arguments, ...

Is extending String class with IsNullOrEmpty confusing?

Everyone knows and love String.IsNullOrEmpty(yourString) method. I was wondering if it's going to confuse developers or make code better if we extend String class to have method like this: yourString.IsNullOrEmpty(); Pro: More readable. Less typing. Cons: Can be confusing because yourString variable can be null and it looks li...

.Net Extension Methods vs Utility Classes

Duplicate of about 20 recent questions; someone please link them. For simple scenarios when should i use one or the other? What are the pros & cons? What are the recommendations for using extension methods? EDIT: Let me give an example. Lets say i have a web relative path represented as a string. Now i want to write a method which...

Getting a parameterless method to act like a Func<ReturnT>

I'm trying to make a part of my code more fluent. I have a string extension that makes an HTTP request out of the string and returns the response as a string. So I can do something like... string _html = "http://www.stackoverflow.com".Request(); I'm trying to write an extension that will keep trying the request until it succeeds. My ...

Fluent sorting of a List<T> on multiple criteria using extension methods?

To sort a List on multiple criteria, I'm currently doing something like: collection.Sort((f1, f2) => { var comp = f1.FirstCriteria.CompareTo(f2.FirstCriteria); return comp != 0 ? comp : f1.SecondCriteria.CompareTo(f2. SecondCriteria); }); But wouldn't it be nice to be able to do something like: collection.MultipleSort(f1.Firs...

ReaderWriterLockSlim Extension Method Performance

I've been playing with collections and threading and came across the nifty extension methods people have created to ease the use of ReaderWriterLockSlim by allowing the IDisposable pattern. However, I believe I have come to realize that something in the implementation is a performance killer. I realize that extension methods are not sup...

Fluent interfaces / extension methods - Turning a flat list into a navigation tree

I currently have an extension Method which converts an IEnumerable of type Tab into a hierarchical collection of TabNodes. // If Tab has no parent its ParentId is -1 public class Tab { public int TabId { get; set; } public string TabName { get; set; } public int Level { get; set; } public int ParentId { get; set; } } publ...