3

I've been reading up on SharePoint 2010 for work and I've noticed that many code examples I run into from books to instructional videos are cast SharePoint objects in a way I never knew existed in C# (and thought was VB exclusive):

SPWeb web = properties.Feature.Parent as SPWeb;

I'm so used to casting (outside of VB) this way (SPWeb)properties.Feature.Parent and was just curious if there was any particular reason most pieces on SharePoint I've encountered use the VB-esque casting notation.

driis
  • 161,458
  • 45
  • 265
  • 341
EHorodyski
  • 774
  • 1
  • 8
  • 30
  • This question really has nothing to do with Sharepoint, you might want to re-tag. I added the C# tag. – driis Feb 09 '12 at 21:34
  • Thanks, this is the only place I've ever seen it pop up so I thought it was just Best Practice for SharePoint. – EHorodyski Feb 09 '12 at 21:37
  • 1
    http://stackoverflow.com/questions/496096/casting-vs-using-the-as-keyword-in-the-clr :) – Simen Echholt Feb 09 '12 at 21:44
  • 1
    You also need to be very careful with sharepoint IDisposable types. There are some cases where it returns you something that you should not dispose. So beware of "using" when coding against sharepoint. – Chriseyre2000 Feb 10 '12 at 09:26

5 Answers5

6

as is called the safe cast operator in C#. There is a semantic difference between that and a normal cast. A safe cast will not throw an exception if the type cannot be cast; it will return null. A normal cast throws InvalidCastException if the type cannot be cast.

In other words, this code assigns null if Parent if not of type SPWeb:

SPWeb web = properties.Feature.Parent as SPWeb;

While the other version throws if Parent is not of the correct type:

SPWeb web = (SPWeb)properties.Feature.Parent;

The as operator can be quite useful if you don't know for sure that an object can be cast to the desired type - in this case it is common to use as and then check for null. as only works on reference types, since value types cannot be null.

This is also explained in this longer article on MSDN.

By the way, the equivalent operator in VB is TryCast (versus DirectCast).

driis
  • 161,458
  • 45
  • 265
  • 341
2
obj as T

is syntax sugar for

obj is T ? (T)obj : null

Thus, it is a "safe" cast. However, it takes longer, in theory. Thus, you should use normal casting unless you specifically want null if an object is not of the expected type. More often, you are better off handling it manually:

if (!(obj is T))
{
    // Handle the case where obj is of an unexpected type.
}

T tobj = (T)obj;
Zenexer
  • 18,788
  • 9
  • 71
  • 77
1

Using the as keyword will set the variable web to null if the Parent is not of type SPWeb.

As where an explicit cast will throw an exception if the Parent is not of type SPWeb.

cory-fowler
  • 4,020
  • 2
  • 18
  • 29
1

"as" is safer than (cast) as it will either return a value as the given type or null. You will find that the following line will (or should) test for null.

Chriseyre2000
  • 2,053
  • 1
  • 15
  • 28
1

If the cast fails the variable assigned to becomes null opposed to throwing an exception 'InvalidCastException '