26.02.2015 Views

C#.Net Migration - A2Z Dotnet

C#.Net Migration - A2Z Dotnet

C#.Net Migration - A2Z Dotnet

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Type Navigation Operators<br />

interface ICowboy {}<br />

interface IArtist {}<br />

class Tex : ICowboy {}<br />

class LaRoche : IArtist {}<br />

// What happens if DrawAndPaint is passed<br />

// a Tex or LaRoche instance?<br />

//<br />

void DrawAndPaint( object o )<br />

{<br />

// InvalidCastException on failure:<br />

IArtist a = (IArtist)o;<br />

a.Paint();<br />

// False on failure:<br />

bool IsArmed = o is ICowboy;<br />

Using explicit cast, is and as<br />

operations to perform type navigation<br />

}<br />

// Null reference on failure:<br />

ICowboy cb = o as ICowboy;<br />

if( cb != null )<br />

{<br />

cb.Draw();<br />

}<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!