extension-methods

Automatic Disposal Extension Method reasonable?

I've been writing some custom WinForm controls which do a pretty heavy amount of drawing and therefore tend to have a lot of disposable graphics based fields lying around (Brushes, Pens, Bitmaps etc..) and as a result my Control's Dispose() method has to call Dispose on each of them. I got concerned that I (or a future maintainer) could...

Split C# collection into equal parts, maintaining sort

I am trying to split a collection into multiple collections while maintaining a sort I have on the collection. I have tried using the following extension method, but it breaks them incorrectly. Basically, if I was to look at the items in the collection, the order should be the same when compared to the broken up collections joined. He...

null target of extension method

public static IFoo Bar<T>(this IFoo target, ...) { // I'm curious about how useful this check is. if (target == null) throw new ArgumentNullException("target"); ... } (1) The code above seems strange to me because I feel like in any case the calling code would be the one to check that. Is there some subtlety to extension met...

Why can you not invoke extension methods directly?

Can someone explain to me why in the following the 3rd invocation of DoSomething is invalid? ( Error message is "The name 'DoSomething' does not exist in the current context" ) public class A { } public class B : A { public void WhyNotDirect() { var a = new A(); a.DoSomething(); // OK this.DoSomething();...

firfox extenation to display image in status bar

i am trying to write my first hello world extension for firefox. its working fine, but i also want to display image in status bar & image is not displaying. my folder structure are as follows helloworld chrome.manifest icon.png install.rdf ->chrome ->content ...

Extension methods and compile-time checking

Maybe a little tricky, but I wonder why. In System.Linq.Enumerable.cs of System.Core.dll we have: public static int Count<TSource>(this IEnumerable<TSource> source); In my code I'm doing something evil: namespace Test { public static class Extensions { public static int Count<TSource>(this IEnumerable<TSource> source) ...

Compliment List of a sub List

listSuper listSub_A listSub_B Is there any extension methods that replace the following piece of code? foreach(int a in listSuper) { if (!listSub_A.Contains(a)) { listSub_B.Add(a); } } In short I want to fill listSub_B with elements in listSuper which are not in listSub_A. ...

Use interface to convert collection of objects with extensions and lambdas

I have some objects like user, address and so on, and Im converting them to business objects using extension methods: public static UserModel ToPresentationForm(this User pUser) { return new UserModel { ... map data ... }; } Also I need to convert strongly typed collections...

encapsulating logic in a linq to sql query via extension method.

Given two classes in your LINQ to SQL .dbml file with the following properies. Customer CustomerId FirstName LastName AddressId Address AddressId Street City State Zip You could construct a LINQ query such as the following. using(var db = new MyDataContext()) { results = db.Customers ....

Converting public method to extension method

In my project's core library we have a very big class, which is tending to become a God object. One of the reasons is that, over a period of time, tasks which should have been in different modules have been put into this class. For ex - class HugeClass{ public void DoModuleXJob(){} public void DoModuleYJob(){} } One of the pro...

Extension method on generic list

I'm quite new to C#, and have two questions regarding generic lists and extension methods. Sorry if the questions are a bit stupid.. What is the difference between: public static IObjectWithTempID FindByTempID (this ObservableCollection<IObjectWithTempID > list, long tempID) and public static IObjectWithTempID FindByTempID< E ...

XNA SpriteBatch Draw Extension Method throws ArrayTypeMismatchException

Hi, I am working on an Animation class for XNA. I decided to try using extension methods for SpriteBatch to add Draw methods that can draw an AnimatedSprite. AnimatedSprite contains a list of Sprite objects, and a Sprite object has a Texture2D image. I created a static Extensions class that contains different overloads of the Draw metho...

Can I create an extension method for System namespace and add it to the GAC?

Hi, In principle, can I create a class library to store my Extension methods for System namespace for example, and add it to the GAC to be usable from any project in my development machine and able to deployment on the GAC of other machines by the setup project ? If yes, Do you think it useful? Thanks in advance. ...

What are some useful examples of extension methods dealing with mathematics and calculations?

My very often used extension method is public static double Pi(double this x) { return Math.PI*x; } in order to have access to 2.0.Pi() or 0.5.Pi() .. etc What are some other examples of mathematics related extension methods the people use often? PS. Just curious. ...

Extension Method with dynamically built linq predicate

Hi there, I have this extension method. public static IQueryable<T> SearchBy<T>(this IQueryable<T> source, SearchCriteria criteria) { //throw an error if the source is null if (source == null) { throw new ArgumentNullException("source"); } ParameterExpression parameter = ...

C# Extension Method for Null or value

How to write an extension method that should check value of the object,if object is null then it should return null otherwise value{without doing casting at receiving end}. something like... public static object GetDefault(this object obj) { if (obj == null) return null; else return obj; } I mean without castin...

C# Extension method for ObjectResult<T> ?

Hi, I use stored procedures in Entity Framework. Let's say that I have that procedure: public static int DoSth(decimal id) { return (int)Adapter.my_proc(id).First(); } as I don't want to get the First() element and then cast it as (int) everytime I'd like to have an extension method which do that for me (gets the F...

Extension method and Regular Method => Error?

I have two methods that are identical. one is public void ExtendFrameIntoClientArea(Window w, int amount) { if (internals.DwmIsCompositionEnabled()) { WindowInteropHelper wi = new WindowInteropHelper(w); internals.DwmExtendFrameIntoClientArea(wi.Handle, new internals.MARGINS(amount)); ...

Problem with Extension Methods and Generic Constraints

I have a base interface and several inherited interfaces. There are extension methods for the base interface that modify the object and return a new instance of the base class (IChildA.Touch() => IBase, IBase.Touch() => IBase). For one inheritance path (IChildB and descendants) i want to implement extension methods that return an objec...

Few GroupJoin in one query

Hi all, I'm trying to write ObjectQuery with few consistent GroupJoin, it means that there should be one main table selection + few additional LEFT JOIN. I'm doing it as following, with SelectMany method, because without it I can't access field RoleID : var routesQuery = entities.Routes.Join( entities.Locales, ...