(C#) why does Visual Studio say it's an object while GetType says it's a Func<object>?

后端 未结 3 1098
眼角桃花
眼角桃花 2021-01-03 14:29

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         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-03 15:00

    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. This is fine, because Func 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" 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)worksheet["A2"], (Func)worksheet["B1"]));
    
    
    

    Now the compiler knows that, even though the [] function returns an object, it can treat it like a Func.

    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.

    提交回复
    热议问题