Cast on C#

J

Jeff Johnson

In VB it's : directcast & tryCast.
What is the equivalent in C# ?

DirectCast = (<data type>) in C#

Example:

int x = (int)someFloatVariable;

I've never heard of TryCast, but I'm pretty sure the equivalent is the "as"
keyword.

SomeObject x = someOtherObject as SomeObject;

If someOtherObject cannot be cast to SomeObject then x will be null.
 
A

Arne Vajhøj

DirectCast = (<data type>) in C#

Example:

int x = (int)someFloatVariable;

I've never heard of TryCast, but I'm pretty sure the equivalent is the "as"
keyword.

SomeObject x = someOtherObject as SomeObject;

If someOtherObject cannot be cast to SomeObject then x will be null.

TryCast does the same.

It was added in VB.NET 2005.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top