extension-methods

What Advantages of Extension Methods have you found? [closed]

此生再无相见时 提交于 2019-11-26 15:08:52
A "non-believer" of C# was asking me what the purpose to extension methods was. I explained that you could then add new methods to objects that were already defined, especially when you don't own/control the source to the original object. He brought up "Why not just add a method to your own class?" We've been going round and round (in a good way). My general response is that it is another tool in the toolbelt, and his response is it is a useless waste of a tool... but I thought I'd get a more "enlightened" answer. What are some scenarios that you've used extension methods that you couldn't

What is the best or most interesting use of Extension Methods you've seen? [closed]

孤街醉人 提交于 2019-11-26 14:58:00
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm starting to really love extension methods... I was wondering if anyone her has stumbled upon one that really blew their mind, or

The operation cannot be completed because the DbContext has been disposed error

℡╲_俬逩灬. 提交于 2019-11-26 14:19:19
问题 I'm new to EF and I'm trying to use an extension method which converts from my Database type User to my info class UserInfo . I'm using database first if that makes a difference? My code below gives the error The operation cannot be completed because the DbContext has been disposed. try { IQueryable<User> users; using (var dataContext = new dataContext()) { users = dataContext.Users .Where(x => x.AccountID == accountId && x.IsAdmin == false); if(users.Any() == false) { return null; } } return

Why is the &#39;this&#39; keyword required to call an extension method from within the extended class

拈花ヽ惹草 提交于 2019-11-26 13:36:12
I have created an extension method for an ASP.NET MVC ViewPage, e.g: public static class ViewExtensions { public static string Method<T>(this ViewPage<T> page) where T : class { return "something"; } } When calling this method from a View (deriving from ViewPage ), I get the error " CS0103: The name 'Method' does not exist in the current context " unless I use the this keyword to call it: <%: Method() %> <!-- gives error CS0103 --> <%: this.Method() %> <!-- works --> Why is the this keyword required? Or does it work without it, but I'm missing something? (I think there must be a duplicate of

Resolving extension methods/LINQ ambiguity

感情迁移 提交于 2019-11-26 12:39:50
问题 I\'m writing an add-in for ReSharper 4. For this, I needed to reference several of ReSharper\'s assemblies. One of the assemblies (JetBrains.Platform.ReSharper.Util.dll) contains a System.Linq namespace, with a subset of extension methods already provided by System.Core. When I edit the code, it creates an ambiguity between those extensions, so that I cannot use OrderBy , for instance. How could I solve this? I would like to use the core LINQ extensions, and not the ones from ReSharper. I get

Interesting “params of ref” feature, any workarounds?

我的梦境 提交于 2019-11-26 12:29:04
问题 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 = true, e; b.SetTo(a, c, d, e); Of course, this does not work because the bools are a value type so they are passed into the function as a value, not as a reference. Other

ArgumentNullException or NullReferenceException from extension method?

[亡魂溺海] 提交于 2019-11-26 12:24:04
What would you consider to be the best exception type to throw when an extension method is called on a null instance (where the extension method does not allow it)? Since extension methods are nothing but static methods you could argue that it should be ArgumentNullException, but on the other hand they're used like instance methods so it might be more natural to use the NullReferenceException. Let's take the following example: public static string ToInvariantString(this IFormattable value, string format) { return value.ToString(format, CultureInfo.InvariantCulture); } This way a

How do I write an extension method in JavaScript?

若如初见. 提交于 2019-11-26 11:55:27
问题 I need to write a few extension methods in JS. I know just how to do this in C#. Example: public static string SayHi(this Object name) { return \"Hi \" + name + \"!\"; } and then called by: string firstName = \"Bob\"; string hi = firstName.SayHi(); How would I do something like this in JavaScript? 回答1: JavaScript doesn't have an exact analogue for C#'s extension methods. JavaScript and C# are quite different languages. The nearest similar thing is to modify the prototype object of all string

Warm-up when calling methods in C#

隐身守侯 提交于 2019-11-26 11:30:27
问题 I just came across this post that talks about time measuring. I remember (I hope I\'m not misremembering) it\'s an unfair competition, if this method is never called before. That is: // At the beginning of the application MyClass instance = new MyClass(); instance.MyMethod(); instance.MyMethod(); // Faster than the first call, because now it\'s warmed up. Do we really have such warming-up theory in C#? If yes, why (what will the CLR do when warming-up)? And is everything the same if this

Why can&#39;t I call an extension method from a base class of the extended type‏?

荒凉一梦 提交于 2019-11-26 11:26:26
问题 I\'m trying add the ability to lookup elements in a List<KeyValuePair<string,int>> by overriding the indexer. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { public class MyList : List<KeyValuePair<string, int>> { public int this[string key] { get { return base.Single(item => item.Key == key).Value; } } } } For some reason, the compiler is throwing this error: \' System.Collections.Generic.List