Quick Interface Question...

  • Thread starter Thread starter z_learning_tester
  • Start date Start date
Z

z_learning_tester

//Let's say the Document class implements the IStorable interface...

class Document : IStorable
{...}

//Then we create a document object...
Document doc = new Document();

//Then we have the following line of code(the part I don't understand)...
IStorable isDoc = doc as Istorable;

Is this last line the same as saying:
IStorable isDoc = (IStorable) doc;

In other words, is the "as" word, just another way of casting?
Thanks,

Jeff
 
Yes, as casts, but rather than throwing an InvalidCastException if it
cannot property type cast from one type to another it sets the reference
to null. So you should always check that the resulting object is not
null before trying to work with it or you will receive a
NullReferenceException.

Thanks,
Steven
 
Oh, so that explains the if(Istorable != Null)... that followed ;-)
If the 'as' check for a valid cast failed, it would return null...
Thanks!

Jeff
 
Thanks for the reference.
I wish C# would last a little longer.
Now I'm afraid that by the time I learn it, Whidbey will be out and I'll
have to start over.
Oh well, that's technology...
Thanks again!

Jeff
 
z_learning_tester said:
Thanks for the reference.
I wish C# would last a little longer.
Now I'm afraid that by the time I learn it, Whidbey will be out and I'll
have to start over.
Oh well, that's technology...

You certainly won't have to start over. C# v2.0 will have things added,
but I don't think much (if anything) will be taken away. Sure, you may
never need to use ArrayList.ToArray(Type) again, but most of what
you're learning now will still be useful.
 
Back
Top