c# - Why am I able to access a non-static 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), raises compile time error on line test.testint
"an object reference required non-static field, method, or property."
but not raise same error on test.testdynamic line. fail, expectedly, runtime error.
the same code raises compile time error on both lines in visual studio 2015.
why same compile time error not raised in visual studio 2013?
you can't access properties / method without creating instance of 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; } }
Comments
Post a Comment