The following code has a static method, Foo()
, calling an instance method, Bar()
:
public sealed class Example
{
int count;
public static void Foo( dynamic x )
{
Bar(x);
}
void Bar( dynamic x )
{
count++;
}
}
It compiles without error* but generates a runtime binder exception at runtime. Removing the dynamic parameter to these methods causes a compiler error, as expected.
So why does having a dynamic parameter allow the code to be compiled? ReSharper doesn't show it as an error either.
Edit 1: *in Visual Studio 2008
Edit 2: added sealed
since it's possible that a subclass could contain a static Bar(...)
method. Even the sealed version compiles when it's not possible that any method other than the instance method could be called at runtime.