extension-methods

Extension methods overloading in C#, does it work?

半城伤御伤魂 提交于 2019-12-30 18:46:12
问题 Having a class that has a method, like this: class Window { public void Display(Button button) { // ... } } is it possible to overload the method with another one that is more broad, like this: class WindowExtensions { public void Display(this Window window, object o) { Button button = BlahBlah(o); window.Display(button); } } What happened when I tried is that I have infinite recursion. Is there a way to make that work? I want the extension method to be called only when the other method can't

C# extension method for a method group

一笑奈何 提交于 2019-12-30 18:32:32
问题 I want to implement an extension method for a method. Consider the following code sample (http://dotnetfiddle.net/HztiOo) : using System; using System.Collections.Generic; public class Program { public static void Main() { A a = new A(); // Noticed that Next() is called twice Console.WriteLine(a.Next(1)); Console.WriteLine(a.Next(1)); // Works var withCache = ((Func<int,int>)a.Next).AddCaching(); withCache = new Func<int,int>(a.Next).AddCaching(); withCache = ExtensionMethods.AddCaching<int

IQueryable Extension: create lambda expression for querying a column for a keyword

ⅰ亾dé卋堺 提交于 2019-12-30 11:39:07
问题 I started with the IQueryable extension methods from this example on CodePlex. What i believe i need is an IQueryable extension method to "Where", where the method signature looks like: public static IQueryable<T> Where<T>(this IQueryable<T> source, string columnName, string keyword) and effectively does this (assuming T.columnName is of type string): source.Where(p => p.ColumnName.Contains("keyword")) using the above CodePlex example, i think i understand how he got the OrderBy method

How to call an extension method of a dynamic type?

Deadly 提交于 2019-12-30 07:51:45
问题 I'm reading the book 'C# in Depth, 2nd Edition' of Jon Skeet. He said that we can call extension methods with dynamic arguments using two workarounds, just as dynamic size = 5; var numbers = Enumerable.Range(10, 10); var error = numbers.Take(size); var workaround1 = numbers.Take((int) size); var workaround2 = Enumerable.Take(numbers, size); Then he said "Both approaches will work if you want to call the extension method with the dynamic value as the implicit this value". I don't know how to

IEnumerable<T> null coalescing Extension

本秂侑毒 提交于 2019-12-29 08:42:28
问题 I frequently face the problem to check whether an IEnumerable<T> is null before iterating over it through foreach or LINQ queries, then I often come into codes like this: var myProjection = (myList ?? Enumerable.Empty<T>()).Select(x => x.Foo)... Hence, I thought to add this extension-method to an Extensions class: public static class MyExtensions { public static IEnumerable<T> AsEmptyIfNull<T>(this IEnumerable<T> source) { return source ?? Enumerable.Empty<T>(); } } A little issue comes

If an extension method has the same signature as a method in the sealed class, what is the call precedence?

旧时模样 提交于 2019-12-29 06:41:08
问题 I was reading about extension methods in C# 3.0. The text I'm reading implies that an extension method with the same signature as a method in the class being extended would be second in order of execution - that is, the method in the sealed class gets called. If this is the case, how can you extend the sealed class ? 回答1: Indeed, the actual method takes precedence over the extension method. And just to make it clear - "order of execution" suggests both might be called; only the original

Usage of Extension Methods

对着背影说爱祢 提交于 2019-12-28 20:34:44
问题 When does using extension methods make sense? Does adding extension methods to a type affect performance? These questions are follow up to the question i asked earlier about Extension Methods. 回答1: When does using extension methods make sense? They make sense when you are using LINQ and want to chain or pipe functional output from one function to another. It improves the readability of the code and allows you to express a concept more elegantly (whatever that is worth). They also allow you to

Is there a performance hit for creating Extension methods that operate off the type 'object'?

时光怂恿深爱的人放手 提交于 2019-12-28 04:00:30
问题 I have a set of extension methods that I regularly use for various UI tasks. I typically define them to run off of type object , even though inside of them I'm typically converting them to string types. public static string FormatSomething(this object o) { if( o != null ) { string s = o.ToString(); /// do the work and return something. } // return something else or empty string. } The main reason I use type object and not string is to save myself in the UI from having to do <%#Eval("Phone")

Will the dynamic keyword in C#4 support extension methods?

ε祈祈猫儿з 提交于 2019-12-27 11:45:54
问题 I'm listening to a talk about C#4 's dynamic keyword and I'm wondering... Will this feature be orthogonal to other .NET features, for example will it support extension methods? public static class StrExtension { public static string twice(this string str) { return str + str; } } ... dynamic x = "Yo"; x.twice(); // will this work? Note: This question was asked before C#4 was shipped which is why it's phrased in the future tense. 回答1: From the "New Features in C# 4" word doc: Dynamic lookup

Will the dynamic keyword in C#4 support extension methods?

旧时模样 提交于 2019-12-27 11:45:40
问题 I'm listening to a talk about C#4 's dynamic keyword and I'm wondering... Will this feature be orthogonal to other .NET features, for example will it support extension methods? public static class StrExtension { public static string twice(this string str) { return str + str; } } ... dynamic x = "Yo"; x.twice(); // will this work? Note: This question was asked before C#4 was shipped which is why it's phrased in the future tense. 回答1: From the "New Features in C# 4" word doc: Dynamic lookup