extension-methods

Multiple from clauses in LINQ

How can this LINQ query expression be re-expressed with extension method calls? public static List<Tuple<int, int>> Concat() { return (from x in Enumerable.Range(1, 3) from y in Enumerable.Range(4, 3) select new Tuple<int, int>(x, y)).ToList(); } ...

F# type extensions in C# project: System.Runtime.CompilerServices.Extension missed?

Hi ;)! I'm diving into F# and it's very fascinating. I'm trying to marry Optional Types and C# like here. Pretty interesting thing... however I miss something important I guess: #light namespace MyFSharp // C# way [<System.Runtime.CompilerServices.Extension>] module ExtensionMethods = [<System.Runtime.CompilerServices.Extension>] ...

When do I have to specify type <T> for IEnumerable extension methods?

I'm a bit confused about the use of all the IEnumerable<T> extension methods, intellisense is always asking for <T>, but I don't think it's necessary to specify <T> at all times. Let's say I have the following: List<Person> people = GetSomePeople(); How is this: List<string> names = people.ConvertAll<string>(p=>p.Name).Distinct<stri...

Adding an extension method to the string class - C#

Not sure what I'm doing wrong here. The extension method is not recognized. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using StringExtensions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ...

ASP.NET MVC 2 Preview 2 - Extend LabelExtensions.LabelFor

I'm wondering if anyone has attempted to write an extension helper to the LabelExtensions.LabelFor HtmlHelper in MVC2? This would be useful for me in that my app requires that I always wrap labels in a <td> tag with a class attribute. Rather than have that code repeated in the View I thought I could write a little extension method: publ...

Extending the Enumerable class in c#?

Hi, I have situation to extend the Enumerable class in c# to add the new Range method that accepts long parameters. I cannot define the method like this public static IEnumerable<long> Range(this Enumerable source, long start, long length) { for (long i = start; i < length; i++) { yield return i; } } Since extens...

Extending classes and instances

This question has two parts. In the Ruby Programming Language book, there is an example (section 8.1.1) of extending a string object and class with a module. First question. Why is it that if you extend a class with a new method, and then create an object/instance of that class, you cannot access that method? irb(main):001:0> module G...

Extension methods in Mono 2.4 and RhinoMocks 3.5

I am playing around with MonoDevelop 2.0 and Mono 2.4 in Ubuntu. I have run into problems with extension methods not being available (eg mockView.Stub(...)) in RhinoMocks 3.5 for AAA style tests. I downloaded the RhinoMocks dll from Ayende's site rather than compiled from source. My project in MonoDevelop is setup to target framework 3....

A different take on FirstOrDefault

The IEnumerable extension method FirstOrDefault didn't exactly do as I wanted so I created FirstOrValue. Is this a good way to go about this or is there a better way? public static T FirstOrValue<T>(this IEnumerable<T> source, Func<T, bool> predicate, T value) { T first = source.FirstOrDefault(predicate); return Equals(first, de...

When should I use HtmlHelper Extension Methods?

I am increasingly finding situations where my ASP.NET MVC view requires some logic to perform layout. These routines have no place being in either my model or my controller. I have 3 options: Write lots of <% %> inline in the view. Write less <% %> in a number of partial views. Write an HtmlHelper Extension method. It is the last opt...

Override (or shadow) a method with extension method?

Is it possible to override or shadow (new in C#) instance methods with extension methods? ...

Is it possible to constrain a generic parameter to be a subtype of the current object?

Here's an interesting problem that I have just come across. It is possible to do what I want using extension methods, but does not seem possible to do with members of the class itself. With extension Methods it is possible to write a method that has a signature that looks like this: public static void DoStuff<T>(this T arg1, T arg2) ...

IEnumerable Extension Methods on an Enum

I have an enum(below) that I want to be able to use a LINQ extension method on. enum Suit{ Hearts = 0, Diamonds = 1, Clubs = 2, Spades = 3 } Enum.GetValues(...) is of return type System.Array, but I can't seem to get access to a ToList() extension or anything else of that sort. I'm just looking to write something like...

How do I write a generic method that takes different types as parameters?

I have the following extension method to add the elements in one collection to another collection: public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> list) { foreach (var item in list) { collection.Add(item); } } This works fine if the IEnumerable list is the same type as the...

What is so great about extension methods?

Possible Duplicate: What Advantages of Extension Methods have you found? All right, first of all, I realize this sounds controversial, but I don't mean to be confrontational. I am asking a serious question out of genuine curiosity (or maybe puzzlement is a better word). Why were extension methods ever introduced to .NET? What b...

which dll contains generics extension methods?

I'm trying to dynamically compile source code using the CodeDom.Compiler stuff, which means I need to reference the basic assemblies manually. The source code that I am compiling must be able to access the basic list extension methods, for for instance, Max(), Min(), or Sum(), and probably lambda expressions as well. When I compile the...

[C#] Interesting "params of ref" feature, any workarounds?

I wonder if there's any way something like this would be possible for value types... public static class ExtensionMethods { public static void SetTo(this Boolean source, params Boolean[] bools) { for (int i = 0; i < bools.Length; i++) { bools[i] = source; } } } then this would be possible: Boolean a = true, b, c = true, d...

Extension Methods in Linq to entity-expressions

Hi, If I create an extension method for my entity objects and try to use it in a LINQ-expression I get an error. Is this a limitation and something I cant do or am I missing something? regards Freddy ...

C# non-vowel words

I want an extension method that needs to return non-vowel words.I designed public static IEnumerable<T> NonVowelWords<T>(this IEnumerable<T> word) { return word.Any(w => w.Contains("aeiou")); } I received error as "T" does not contain extanesion method "Contains". ...

C# - Sorting using Extension Method

I want to sort a list of person say List<Person> persons=new List<Person>(); persons.Add(new Person("Jon","Bernald",45000.89)); persons.Add(new Person("Mark","Drake",346.89)); persons.Add(new Person("Bill","Watts",456.899)); based on public enum CompareOptions { ByFirstName, ByLastName, BySalary } public enum SortOrd...