C# newbie question here. The following code (taken from the book \"C# From Novice to Professional\" by Christian Gross, Apress) gives an error:
worksheet.Ad
You need to understand the difference between dynamic typing and static typing. The indexer for your worksheet
object most likely has a static type of object
.
public object this[string cell]{get{...}set{...}}
Because all objects in C# inherit from type object
, the object reference stored in a cell can be a reference to any object.
That is, because a delegate (such as Func<T>
) is an object
, it can be stored in an object
reference:
Func<object> func = ()=>return "foo";
object o = func; // this compiles fine
And the compiler can figure this all out, because it understands implicitly that a derived class can be stored in a reference to a base class.
What the compiler cannot do automatically, is determine what the dynamic type, or run time type of an object is.
Func<object> func = ()=>return "foo";
object o = func; // this compiles fine
func = o; // <-- ERROR
The compiler doesn't know that the object
stored in o
is actually of type Func<object>
. It's not supposed to keep track of this. This is information that must be checked at run time.
func = (Func<object>)o; // ok!
The above line of code compiles into something that behaves similarly to this:
if(o == null)
func = null;
else if(typeof(Func<object>).IsAssignableFrom(func.GetType()))
__copy_reference_address__(func, o); // made up function! demonstration only
else throw new InvalidCastException();
In this way, any cast (conversion from one type to another) can be checked at run time to make sure it's valid and safe.
Others have given accurate and detailed answers, but here I will try to explain in simple language.
When you write worksheet["A2"]
you really are calling a member function of worksheet
worksheet
has a member function named []
that accepts a string
and returns an object
The signature of the member function []
looks like object this[string id]
So the function worksheet["A2"]
returns something that is an object
. It could be an int
or a string
or many other things. All the compiler knows is that it will be an object
.
In the example, you have it returning a Func<object>
. This is fine, because Func<object>
is an object
. However, you then pass the result of that function in as a parameter to another function.
The problem here is that the compiler only knows that worksheet["A2"]
returns an object
. That is as specific as the compiler can be.
So the compiler sees that worksheet["A2"]
is an object, and you are trying to pass the object to a function that does not accept object
as a parameter.
So here you have to tell the compiler "hey dummy, that's a Func<object>
" by casting the returned object to the correct type.
worksheet.Add("C3", CellFactories.DoAdd(worksheet["A2"], worksheet["B1"]));
can be re-written as
worksheet.Add("C3", CellFactories.DoAdd((Func<object>)worksheet["A2"], (Func<object>)worksheet["B1"]));
Now the compiler knows that, even though the []
function returns an object
, it can treat it like a Func<object>
.
side note: You're probably doing too much on one line. That may be hard for people to read in the future.
Why does the called method 'see' the argument as a different type than the GetType() returns?
The compiler only knows that worksheet[]
returns an object. The compiler can not call GetType()
on it at compile time.
What good is the GetType() method?
There are quite a few uses and abuses of the GetType()
method, but that is an entirely different discussion. ;)
In summary, the compiler does not assume anything about types. This is a good thing because you get a compile time error when you try to fit this square peg into a round hole. If the compiler did not complain, this error would surface at run-time, which means you would probably need a unit test to detect the problem.
You can get around this problem by telling the compiler "I know for a fact that this thing is a round peg, trust me." and then it will compile. If you lie to the compiler, you will get a run-time error when that code is executed.
This is called "static typing". The opposing philosophy is called "dynamic typing" where type checks are done at run-time. Static vs dynamic is a lengthy debate and you should probably research it on your own if you're interested.
VS claims that both args in the method call above are of type object whereas the method accepts only Func. But the value of both worksheet elements is of type Func
Yes, but the declared type is object
. The compiler can't know that the actual runtime type will be Func<object>
, so an explicit cast is necessary.