Home » none » Forcing Extension Methods to Be Used

Forcing Extension Methods to Be Used

tobj-067 I received the following question:

Is it possible to force a namespace to be included so that the extension methods in it will be used in it instead of the methods in the main class?

I have to admit, I had to go and do a little research on this, but once I did, I found out that the assumption behind the question is inaccurate.

Rule number one of extension methods is that if an instance method exists on a class, that method will take priority over any extension method that might otherwise be called.

So if you wanted to provide an extension method that converts a number to a string using the ToString() method by creating your own ToString() extension method that acts on an integer to format it some specific way, you will still get the default ToString() method when you call ToString().

Further, if you have two extension methods that could be called and one is in the current namespace and one is in another namespace, the one in the current namespace will be called first.

Finally, if you have two methods in two different namespaces, the normal rules regarding which method will get called come in to play.

So, to answer the original question, no, you can’t force a namespace to be used.  And even if you could, it wouldn’t solve your problem.

Other places talking about Extension Methods:

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Why you can’t cast an integer to a string.Why you can’t cast an integer to a string. I saw this question last Thursday on Channel 9 that I've heard before. My guess is that there are enough people who have the same question that it's worth addressing. I know there's […]
  • Extension Methods in CSharpExtension Methods in CSharp Have you ever wanted to add a method to a sealed class, like string?  Or, have you ever wished you could provide more than a signature for a method in an interface?Well now you can.  […]
  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]
  • Which Language VB or CSharp?Which Language VB or CSharp? I wrote the following article a long time ago on my main domain, but recently I had a reader send in a question that this article addresses.  So I’m putting this article here so that it is […]
  • Reflection – Accessing MethodsReflection – Accessing Methods Today is the next to last installment on reflection.  We will look at how to call methods and pass parameters.  This is going to look a lot like what we've already done in […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback

  • Pingback: Dew Drop - December 1, 2008 | Alvin Ashcraft's Morning Dew()

  • http://danvanderboom.com Dan Vanderboom

    What I do sometimes is to define the extension method in the same namespace as the type I’m extending. For example, if I want to add an extension method to System.Windows.Forms.Control, I can define it within namespace System.Windows.Forms. Then, so long as I have a reference to that assembly, the extension will always be available, and no additional using/import statement will be needed.