.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

Forcing Extension Methods to Be Used

December 01, 2008 By: Dave

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:

  • Generic Extension Method to Unit Test INotifyPropertyChanged
  • Two Extension Methods For DevExpress.XtraBars.PopupMenu « M.Efe …
  • ‘In’ Operator for .NET Framework

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • .Net String Pool – Not Just For The Compiler
  • Why you can’t cast an integer to a string.
  • Using VB.NET From CSharp
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: .net, csharp, extension methods, vb.net

2 Responses to “ Forcing Extension Methods to Be Used ”

  1. # 1 Dew Drop - December 1, 2008 | Alvin Ashcraft's Morning Dew Says:
    December 1st, 2008 at 9:30 am

    [...] Forcing Extension Methods to Be Used (Dave M. Bush) [...]

  2. # 2 Dan Vanderboom Says:
    December 1st, 2008 at 10:51 am

    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.

← SQL – Filtering WHERE condition on two rows
jQuery – Events →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    December 2008
    S M T W T F S
    « Nov   Jan »
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.