Static extension methods [duplicate]

In short, no, you can’t. Long answer, extension methods are just syntactic sugar. IE: If you have an extension method on string let’s say: public static string SomeStringExtension(this string s) { //whatever.. } When you then call it: myString.SomeStringExtension(); The compiler just turns it into: ExtensionClass.SomeStringExtension(myString); So as you can see, there’s no way to … Read more

Why is the ‘this’ keyword required to call an extension method from within the extended class

A couple points: First off, the proposed feature (implicit “this.” on an extension method call) is unnecessary. Extension methods were necessary for LINQ query comprehensions to work the way we wanted; the receiver is always stated in the query so it is not necessary to support implicit this to make LINQ work. Second, the feature … Read more

ArgumentNullException or NullReferenceException from extension method?

In general, exceptions included, you should treat an extension method as if it were a normal static method. In this case you should throw an ArgumentNullException. Throwing a NullReferenceException here is a bad idea for a few reasons A null reference did not actually occur so seeing one is counterintuitive Throwing a NullReferenceException and causing … Read more

Razor HtmlHelper Extensions (or other namespaces for views) Not Found

Since the Beta, Razor uses a different config section for globally defining namespace imports. In your Views\Web.config file you should add the following: <configSections> <sectionGroup name=”system.web.webPages.razor” type=”System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″> <section name=”host” type=”System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ requirePermission=”false” /> <section name=”pages” type=”System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ requirePermission=”false” /> </sectionGroup> </configSections> <system.web.webPages.razor> <host factoryType=”System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, … Read more