2

Why the conversions between compatible reference types will compile (Excel 2010, .Net 4.5) in this case

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excelApplication = null;
excelApplication = new Excel.Application();

Excel.Worksheet worksheet = workbook.Worksheets[1] as Excel.Worksheet;

and in the case below it will not, although I saw exampales shown like that:

Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];

In this case I get the following compiling error :

> CSC : error CS0518: Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not 
defined or imported

> error CS1969: One or more types required to compile a dynamic expression cannot be 
found. Are you missing a reference?

Best,

EDIT : Thanks to the both answerer below the following explanation sounds reasonable:

without including Microsoft.CSharp in the Project References for projects with .Net Version >= 4.0, support for inter operation between the Dynamic Language Runtime (DLR) and C# is not possible, i.e. no dynamic cast is possible.

Guanxi
  • 3,103
  • 21
  • 38
HeinrichStack
  • 694
  • 3
  • 8
  • 28
  • `as` returns null, the explicit cast thrown an exception. – asawyer Nov 30 '12 at 16:51
  • @asawyer That's only if it's not of the proper type. If you know it's always of the proper type they're effectively the same. – Servy Nov 30 '12 at 16:52
  • if you put it like this Excel.Worksheet worksheet = ((Excel.Worksheet)workbook.Worksheets[1]); then it ll work – Azhar Khorasany Nov 30 '12 at 16:52
  • @Servy Sure that goes without saying. – asawyer Nov 30 '12 at 16:53
  • does `Excel.Worksheet worksheet = (Excel.Worksheet)(workbook.Worksheets[1]);` work? – Sam I am says Reinstate Monica Nov 30 '12 at 16:53
  • 1
    @asawyer It doesn't go without saying when you're saying it to someone who doesn't know what the differences between the two operators are. – Servy Nov 30 '12 at 16:54
  • @Servy I'm not disagreeing with you, my comment was in response to the specific question is all. – asawyer Nov 30 '12 at 16:55
  • @HeinrichStack - you mentioned that your program does not compile. I tried a quick sample and was able to compile and run with both casting and `as`. I suspect your true error is something a bit different. If you post a more complete sample of your code, we might be able to help (for example, wehere does your workbook variable come from?). And are you sure you're not declaring `worksheet` twice? – Jon B Nov 30 '12 at 17:40
  • @HeinrichStack Given the error messages you provided, it seems likely that the application is targeting ASP.NET 3.5. My guess is that going to the project properties and setting the target framework to 4.0 will solve the issue. – Miltos Kokkonidis Dec 03 '12 at 07:40

4 Answers4

4

(T) x will throw an InvalidCastException exception if x cannot be casted to T, whereas x as T will return null in this case. When there is no type casting problem the two are equivalent.

(T) x is a simpler and faster operation than x as T. For performance results have a look at : http://www.codeproject.com/Articles/8052/Type-casting-impact-over-execution-performance-in#_rating

Some additional information:

A cast explicitly invokes the conversion operator from one type to another. The cast will fail if no such conversion operator is defined. You can write custom conversion operators to convert between user-defined types. For more information about defining a conversion operator, see explicit (C# Reference) and implicit (C# Reference).

Source: http://msdn.microsoft.com/en-us/library/ms173105(v=vs.80).aspx

UPDATE: Given the error messages you provided, it seems likely that the application is targeting ASP.NET 3.5. My guess is that going to the project properties and setting the target framework to 4.0 will solve the issue.

Miltos Kokkonidis
  • 3,888
  • 20
  • 14
  • So, Excel.Worksheet doesnt have a conversion operator and thats why the `(Excel.Worksheet)workbook.Worksheets[1];` will not compile ? – HeinrichStack Nov 30 '12 at 17:19
  • Actually, `(Excel.Worksheet)workbook.Worksheets[1];` *should* work. There must be something else going on. If you have something that compiles and just modify this line, it should still compile. If you are still getting an error please post your code and the exact error message you are getting and hopefully I will be able to help. – Miltos Kokkonidis Nov 30 '12 at 22:33
  • Miltiadis , I inserted the error messages. What could you tell more about it? – HeinrichStack Dec 03 '12 at 07:18
  • Seems to be an ASP.NET 3.5 issue. Updated the answer. Hope that helps. If it does please accept the answer and I will provide more information about what I believe went wrong. – Miltos Kokkonidis Dec 03 '12 at 07:48
  • Well, the `Target framework` was `.NET Framework 4.5`. I set it to `4.0` but still the same error messages appeared. – HeinrichStack Dec 03 '12 at 09:46
1

What does as actually return? If it's returning null, it's because it cannot cast to Excel.Worksheet. Casting works differently, and will error if you cannot cast. This is probably what is happening here.

Jon B
  • 51,025
  • 31
  • 133
  • 161
  • Casting will throw a `InvalidCastException` to be specific. – Powerlord Nov 30 '12 at 16:53
  • No, it doesnt return 'null'. While the last doesnt compile. – HeinrichStack Nov 30 '12 at 17:16
  • @HeinrichStack that is crucial information you left out of your question. You should edit your question to add the actual error message you are receiving. – Jon B Nov 30 '12 at 17:21
  • Jon, I inserted the error messages. What could you tell more about it? – HeinrichStack Dec 03 '12 at 07:18
  • @HeinrichStack - does your project have a reference to `Microsoft.CSharp`? – Jon B Dec 03 '12 at 13:17
  • Well, that was it. Thanks a lot. It will be hard for me to put an mark only one answer as both of you contributed. Thats why I will remain with my upvotes to you both for the sake of equlibrium but with a big thanks to everyone... So here the explanation: Which was missing `Microsoft.CSharp` reference in the project. MS doesnt say a lot about this class, mainly for compilation of C#. So, this problem could be answered like this : **without including CSharp for .Net Version > 4.0, support interoperation betwen the dynamic language runtime (DLR) and C# is not possible**. Best – HeinrichStack Dec 03 '12 at 14:06
0

As - will return null if the cast fails at run time. So this is safe.

() - will throw exception if the cast fail at run time

As can be only applied to reference types or nullable types.

As cast is faster than explicit cast. Hope this helps!!

Nirmal kumar
  • 9
  • 1
  • 1
0

There are clear-cut cases where cast or as are in order, but sometimes they look as if they are interchangeable. In those cases, when both could be used, I'd stop a second to think about readability:

Use type casting whenever possible, by default. Reserve as for those cases when you want to explicitly express that you are converting references because of Object-Oriented reasoning, leveraging inheritance or interfaces.

pid
  • 11,472
  • 6
  • 34
  • 63