Why can I access a non-stationary method in a static context when using dynamic

The following code:

class Program
{
    static void Main(string[] args)
    {
        dynamic d = 0;

        int x = Test.TestDynamic(d);
        int y = Test.TestInt(0);
    }
}

public class Test
{
    public int TestDynamic(dynamic data)
    {
        return 0;
    }

    public int TestInt(int data)
    {
        return 0;
    }
}

      

when run in Visual Studio 2013 Update 5 throws a compile time error on line with Test.TestInt

"An object reference is required for a non-static field, method, or property."

but doesn't throw the same error on the Test.TestDynamic test line. It is not expected to execute a runtime error.

The same code throws a compile-time error on both lines in Visual Studio 2015.

Why does Visual Studio 2013 have the same compile-time error?

+3


source to share


1 answer


You cannot access properties / method without instantiating the object.



class Program
{
    static void Main(string[] args)
    {
        dynamic d = 0;

        Test test = new Test();
        int x = test.TestDynamic(d);
        int y = test.TestInt(0);
    }
}

public class Test
{
    public int TestDynamic(dynamic data)
    {
        return 0;
    }

    public int TestInt(int data)
    {
        return 0;
    }
}

      

-1


source







All Articles