Cast on C#

  • Thread starter Thread starter Mr. X.
  • Start date Start date
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.
 
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
 
Back
Top