This question already has an answer here:
-
Casting vs using the 'as' keyword in the CLR
18 answers
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.
(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.
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.
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!!
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.