Came across this snippet of code today:
EventFeed feed = null;
feed = service.Query(eventQuery) as EventFeed;
Why the as EventFeed
at the end? The return type of that function is already an EventFeed
, so I'm struggling to see the benefit of such a statement.
I found it difficult to search for this problem so I'm asking on here. What are the advantages to writing a line like this?
feed
might be declared as EventFeed
however the result of service.Query(eventQuery)
may not be.
Using as
stops an exception from being thrown and you end up with null
instead if the result of the expression cannot be cast as EventFeed
.
You can read more about as
here - http://msdn.microsoft.com/en-us/library/cscsdfbt(v=vs.71).aspx
Your query may be returning an object
service.Query(eventQuery)
so you are casting this object as your data type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With