Home

Compilation error when combining static usings and extension methods


// Additional namespace import:
// static UserQuery.StaticUsingsTest

void Main()
{
	Uppercase("linqpad").Dump();
}

public static class StaticUsingsTest
{
	public static string Uppercase(string value) => value.ToUpper();
}
This works fine, however as soon as you add an extensions method, the code no longer compiles

// CS0426 The type name 'StaticUsingsTest' does not exist in the type 'UserQuery'

void Main()
{
	Uppercase("linqpad").Dump();
}

public static class StaticUsingsTest
{
	public static string Uppercase(string value) => value.ToUpper();
}

public static class Extensions
{
	public static string Lowercase(this string value) => value.ToLower();
}
See the linqpad below for this example:
http://share.linqpad.net/9d88uv.linq

Comments

  • In your using declarations, you need to change

    static UserQuery.StaticUsingsTest

    to

    static StaticUsingsTest

    This is because when you define extension methods, LINQPad moves your classes outside UserQuery because C# does not allow nested types to define extension methods.

    Note that for consistency, you can force LINQPad to move all types outside UserQuery - regardless of whether there are extension methods present - with the following directive:

    #define NONEST
Sign In or Register to comment.