C # extension method doesn't seem to exist
I cannot find the following extension method that can be found in another class in the same namespace ( MyProject.Util
).
using System.Collections.Specialized;
namespace MyProject.Util
{
public static class Extensions
{
public static string Get(
this NameValueCollection me,
string key,
string def
)
{
return me[key] ?? def;
}
}
}
As you can see, this is basically a different version foo[bar] ?? baz
, but I still don't understand why VS2008 fails to compile, telling me that neither version Get
takes two arguments.
Any ideas?
+2
a source to share
6 answers
The following seems to work for me ...
using System.Collections.Specialized;
namespace MyProject.Util
{
class Program
{
static void Main(string[] args)
{
var nvc = new NameValueCollection();
nvc.Get( )
}
}
}
namespace MyProject.Util
{
public static class Extensions
{
public static string Get(
this NameValueCollection me,
string key,
string def
)
{
return me[key] ?? def;
}
}
}
Have you checked the target structure?
+1
a source to share
Works well when I try. There really is only one failure mode: forget to add a using statement for the namespace that contains the extension method:
using System.Collections.Specialized;
using MyProject.Util; // <== Don't forget this!
...
var coll = new NameValueCollection();
coll.Add("blah", "something");
string value = coll.Get("blah", "default");
+1
a source to share