extension-methods

AddRange to a Collection

There are some similar questions to this; I did not find one that I felt answered this use case. (Please feel free to correct me!) A coworker asked me today how to add a range to a collection. He has a class that inherits from Collection<T>. There's a get-only property of that type that already contains some items. He wants to add the i...

Why is System.Windows.Shape.Path sealed?

I was trying to extend the shape class to contain an additional variable but have found the class is sealed. How can I achive this simply, using an alternate implementation approach? Is creating a new class and storing a shape an passing all the method calls through the easiest approach; I'm sure there is a better way though? ...

LINQ - Distinct() returning a different value if extension method used

I have a LINQ query which is attempting to get all of the distinct months of all of the dates in a table. I had this working using the Distinct() extension method. I then made it more readable by using an extension method to extract the month. And then it stopped returning Distinct results. Can anyone help me work out what happened her...

Extension Methods - IsNull and IsNotNull, good or bad use?

I like readability. So, I came up with an extension mothod a few minutes ago for the (x =! null) type syntax, called IsNotNull. Inversly, I also created a IsNull extension method, thus if(x == null) becomes if(x.IsNull()) and if(x != null) becomes if(x.IsNotNull()) However, I'm worried I might be abusing extension methods. Do yo...

How to ensure HTTPContext has value?

I am building a version of the MVC Storefront. I have two themes, one is the standard ui, and the other is the admin ui. I have some HTML Helper methods, which deliver .ascx files to the .aspx files just like the example, but without plug-ins. All the stuff works on the default theme, but none of it works in the admin theme. I always ge...

C#, WinForms and Extension Methods

The Question Aside from all the obvious answers, what would cause extension methods to generate compiler errors like this one: 'DataType' does not contain a definition for 'YourExtensionMethodName' I've got a real stumper here, and it's spelled out for you in detail below. I've exhausted all the possible causes I can think of. Scenar...

Using extension methods in .NET 2.0 ?

I want to do this, but getting this error: Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? [snipped some path stuff] I have seen some answers here that says, you have to define this attri...

How do I create an extension method (F#)?

How do I create an extension method in F#, for example, like this C# extension: public static string Right(this string host, int index) { return host.Substring(host.Length - index); } EDIT Basically a duplicate: http://stackoverflow.com/questions/702256/f-extensions-in-c (Asks a more in-depth question, but also...

Operators as method parameters in C#

I don't think it's possible to use operators as a parameters to methods in C# 3.0 but is there a way to emulate that or some syntactic sugar that makes it seem like that's what's going on? I ask because I recently implemented the thrush combinator in C# but while translating Raganwald's Ruby example (1..100).select(&:odd?).inject(&:+)....

Extension methods for specific generic types

I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me: What I want to do is something like the following: type IEnumerable<int> with member this.foo = this.ToString() Yet it gives me the compiler error (underli...

FindAll vs Where extension-method

I just want know if a "FindAll" will be faster than a "Where" extentionMethod and why? Example : myList.FindAll(item=> item.category == 5); or myList.Where(item=> item.category == 5); Which is better ? ...

c# - Why can't my Dictionary class see the ToArray() method?

Hi, I see in the API Dictionary has a ToArray() method (in the extension classes area), but when I try to use this from my Dictionary instance it can't see it??? How do I "enable" ToArray() for my Dictionary instance? Thanks ...

ICollection / ICollection<T> ambiguity problem

Just want to make simple extension for syntactic sygar : public static bool IsNotEmpty(this ICollection obj) { return ((obj != null) && (obj.Count > 0)); } public static bool IsNotEmpty<T>(this ICollection<T> obj) { return ((obj != null) && (obj.Count > 0)); } It works perfectly when I work with some collectio...

Can C# extension methods access private variables?

Is it possible to access an object's private variables using an extension method? ...

Extensions library in C#

Hello everybody. I am looking for a good Extensions library (library with a great number of extensions methods) in C# for standard type. I have found some libraries on Codeplex, but they are considered to be very weak by me. Edit: I need a library which implements different mathematical functions for different (more the better) purposes...

Are C# extension methods only available for instance methods?

I have recently started to make useful use of C# extension methods. The SO examples and the documentation suggest that they are only used for instance methods (i.e. with the this keyword). It is possible to use them (or another approach) with static/class methods? (My particular requirement is converting Java code to C# where "most of ...

C# - Returning an Enum? from a static extension method

I've added some extension methods for strings to make working with some custom enums easier. public static Enum ToEnum<T>(this string s) { return (Enum)Enum.Parse(typeof(T), s); } public static bool IsEnum<T>(this string s) { return Enum.IsDefined(typeof(T), s); } Note -- because of limitations of generic type constraints, I ...

using generics in c# extension functions

I am using generics to translate Java code to C# and having trouble with containers of the sort: public static class MyExtensions { public static void add(this List<object> list, object obj) { list.Add(obj); } public static void add(this List<string> list, string s) { list.Add(s); } } It seems t...

LINQ: Checking if a field (db) contains items in an ILIST ?

Hi there, I am trying to create an extension method that i can forward a IList of statuses and check weather they exist, I thought the best way to do this was an ILIST - but maybe i wrong? Is this the best way to pass multple items to a method - A List? Its a generic LIST so hence no conversion etc from Object. Basically I have this as...

Extension methods + Console apps

Ok, i'm well and truly stumped here. I've written extension methods before, and never had any problems. However, i've never had to use them in Console Apps. The following code won't compile and I have no idea why! I created a simple console app to try it out and it just won't work: using System; using System.Collections.Generic; using ...