No type inference with generic extension method

UPDATE from November 2020: The original answer below was written in 2011; the rules for generic method type inference, overload resolution, and how “final validation” of methods is done have had small but significant changes in recent versions of C#; this answer, and the link to an archived article on my original MSDN blog about … 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

Operator Overloading with C# Extension Methods

This is not currently possible, because extension methods must be in static classes, and static classes can’t have operator overloads. But the feature is being discussed for some future release of C#. Mads talked a bit more about implementing it in this video from 2017. On why it isn’t currently implemented, Mads Torgersen, C# Language … 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

Extension methods must be defined in a non-generic static class

change public class LinqHelper to public static class LinqHelper Following points need to be considered when creating an extension method: The class which defines an extension method must be non-generic, static and non-nested Every extension method must be a static method The first parameter of the extension method should use the this keyword.

Impossible to use ref and out for first (“this”) parameter in Extension methods?

You have to specify ref and out explicitly. How would you do this with an extension method? Moreover, would you really want to? TestClass x = new TestClass(); (ref x).ChangeWithExtensionMethod(otherTestClass); // And now x has changed? Or would you want to not have to specify the ref part, just for the first parameter in extension … Read more

Detect target framework version at compile time

The linked SO question with ‘create N different configurations’ is certainly one option, but when I had a need for this I just added conditional DefineConstants elements, so in my Debug|x86 (for instance) after the existing DefineConstants for DEBUG;TRACE, I added these 2, checking the value in TFV that was set in the first PropertyGroup … Read more

Convert string[] to int[] in one line of code using LINQ

Given an array you can use the Array.ConvertAll method: int[] myInts = Array.ConvertAll(arr, s => int.Parse(s)); Thanks to Marc Gravell for pointing out that the lambda can be omitted, yielding a shorter version shown below: int[] myInts = Array.ConvertAll(arr, int.Parse); A LINQ solution is similar, except you would need the extra ToArray call to get … Read more

tech