extension-methods

Is there a way to make "destructive" string methods a-la Ruby?

In Ruby, methods which change the object have a bang on the end: string.downcase! In c# you have to do: foo = foo.ToLower() Is there a way to make an extension method like: foo.ConvertToLower() that would manipulate foo? (I think the answer is no since strings are immutable and you can't do a ref this in an extension method.) ...

Is there an easier way of using extension methods in Powershell v2

Background this post explains how one can consume extension methods in Powershell http://community.bartdesmet.net/blogs/bart/archive/2007/09/06/extension-methods-in-windows-powershell.aspx Compare this to what someone does in C# - they add a "using SomeAssembly" and all the extension methods are loaded. My questions Did this get sim...

What's the best strategy for get/setting metadata on Ruby methods at runtime?

I have a class with some methods. It's supersekret but I've reproduced what I can here. Class RayGun # flashes red light # requires confirmation # makes "zowowowowowow" sound def stun! # ... end # flashes blue light # does not require confirmation # makes "trrrtrtrtrrtrtrtrtrtrtr" sound def freeze! # ... end...

Resolving extension methods/linq ambiguity

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 extensio...

ArgumentNullException or NullReferenceException from extension method?

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...

MVC helper extension

why do i get the error " Expected class, interface, enum or struct" with string underlined? public static string IsSelected(this HtmlHelper helper, string A, string B) { return "wtf"; } ...

extension method as a way to rename property

Some class has ugly field called URL, but Id rather call it file. public static void setFile(this AxWMPLib.AxWindowsMediaPlayer mp, string filename) { mp.URL = filename; } and public static string file(this AxWMPLib.AxWindowsMediaPlayer mp) { return mp.URL; } Is there any way to not use setFile, but make file act like p...

Official LINQ Extension Methods

With .NET 3.5 a large amount of extension methods were added to the core base of code. I've noticed that in MSDN, IEnumerable<> etc have a section on Extension methods that have been added. Is there a list of ALL the extension methods that have been added for reference? EDIT Thanks for the answers but I'm looking for a full list, not ...

How do you extend (or CAN you extend) the static Math methods?

With C# 3.0, I know you can extend methods using the 'this' nomenclature. I'm trying to extend Math.Cos(double radians) to include my new class. I know that I can just create a "Cos" method in my existing class, but I'm just interested in seeing how/if this can be done for the sake of the exercise. After trying a few new things, I'm re...

What Advantages of Extension Methods have you found?

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...

Extending the String Class With Properties?

I have an application where I need to populate a textbox with a company name and I have filled a custom AutoCompleteStringColection with all the available company names from the database. When a user enters changes the company name by typing and selecting from the list a new company name I need to have the id (Guid), of the selected comp...

select child object collection with lambdas

I have the following class objects: public class VacancyCategory { public int ID { get; set; } public string Text { get; set; } public IList<VacancySubCategory> SubCategories { get; set; } } public class VacancySubCategory { public int ID { get; set; } public string Text { get; set; } public VacancyCateg...

How to chain views in Django?

I'm implementing James Bennett's excellent django-contact-form but have hit a snag. My contact page not only contains the form, but also additional flat page information. Without rewriting the existing view the contact form uses, I'd like to be able to wrap, or chain, the views. This way I could inject some additional information vi...

Trouble with type inference in writing generic extension method

I really hate sometimes how IDictionary<TKey, TValue> [key] will throw an exception if the key doesn't exist in the dictionary. Of course there is TryGetValue(), but that seems to have been optimized for performance and not usability. So I thought, oh I'll just make an extension method for it - which I did : public static class Collec...

Extension methods in Python

Does Python have extension methods like C#? Is it possible to call a method like: MyRandomMethod() on existing types like int? myInt.MyRandomMethod() ...

How can I apply a common extension method to multiple unrelated types in a third party SDK?

Hi guys, I'm beginning to fall in love with Extension Methods, but I just don't know how to create an EM only for a determinate Object type. I have for example: public static void AddPhoneNumberToContact(this Contact contact, PhoneType type, String number) { lock (contact) { PhoneRow pr = PhoneRow.CreateNew(); ...

Problem with C# serialization extension methods

I really like the extension method that TWith2Sugars posted here. I ran into an odd problem though. When I put this into a shared class library and call the serialization function, I get the following error: The type MyType was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known stati...

LINQ dependent calculation assignment

I'm using the following pattern in C#: IList<foo> x = y.Select(a => new foo { b = Calc1(), c = Calc2() }).ToList(); foreach(foo f in x) { f.d = b / c; } What I would like to do though is: IList<foo> x = y.Select(a => new foo { b = Calc1(), c = Calc2() d = b / c; }).ToList(); So the question is: How can you ...

Can I "multiply" a string (in C#)?

Suppose I have a string, for example, string snip = "</li></ul>"; I want to basically write it multiple times, depending on some integer value. string snip = "</li></ul>"; int multiplier = 2; // TODO: magic code to do this // snip * multiplier = "</li></ul></li></ul>"; EDIT: I know I can easily write my own function to impleme...

Is this a good use of an ExtensionMethod?

I just wrote an if statement in the lines of if (value == value1 || value == value2 || value == value3 || value == value4) //do something and got annoyed that I always have to repeat the 'value ==' part. In my opinion this is serving no purpose other than making it difficult to read. I wrote the following ExtensionMethod that sho...