Archives for: September 2010

09/02/10

Permalink 06:23:19 pm, by truewill Email , 260 words, 419 views   English (US)
Categories: Tips, C#, .NET, Tools

LINQPad tip

I love LINQPad. It is incredibly useful for quick .NET coding spikes (whether or not they use LINQ) and for ad-hoc database queries (although I wish it were easy to set the isolation level). I’ve purchased and recommend the autocompletion (IntelliSense), and our office recently bought a pack of licenses too.

I found a limitation today in trying to answer a Stack Overflow question, along with a workaround.

C# extension methods cannot be defined in a nested class. LINQPad wraps your code in a class behind the scenes. It’s smart enough to detect “nested” static extension-hosting classes, and appears to exclude those from its internal class. This works well - unless your extension methods reference other classes nested in LINQPad:

// Set language combobox to "C# Program"

void Main()
{
    new Local().AddFoo().Dump();
}

// Really a nested class
public class Local {}

public static class Extensions
{
    // Type or namespace name 'Local' could not be found
    public static string AddFoo(this Local local)
    {
       return local.ToString() + "Foo";
    }
}

The workaround is to complete the hidden wrapper class with a closing brace, then leave off the last closing brace (since LINQPad will add one):

void Main()
{
    new Local().AddFoo().Dump();
}
}  // Extra closing brace

public class Local {}

public static class Extensions
{
    public static string AddFoo(this Local local)
    {
       return local.ToString() + "Foo";
    }
// Leave off the last closing brace

I figured this out based on the article Add Extension Methods in LinqPad. This is tested with LINQPad v4.20. You can vote for my feature suggestion if you like. (Personally I’d rather you voted for setting the isolation level.)

Development Central

Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.

Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.

Search

Categories

Linkblog

b2evolution

contributors

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 1

powered by b2evolution free blog software