Share via


What's the C# version of DirectCast?

Question

Thursday, June 8, 2006 7:08 PM

This is the VB version:

Dim t As TextBox = DirectCast(FormView1.FindControl("DataTextBox"), TextBox)

  t.Text = "hello"

How do I do the same thing in C#? I don't see the DirectCast available in C#.

All replies (8)

Thursday, June 8, 2006 7:18 PM âś…Answered

TextBox t = (TextBox)o;
t.Text = "hello";

Where o is a reference to the object you want to typecast.

So for yours...

TextBox t = (TextBox)FormView1.FindControl("DataTextBox");
t.Text = "hello";


Thursday, June 8, 2006 7:25 PM

Thanks!


Thursday, October 11, 2012 10:46 PM | 1 vote

That's not the equivalent of the VB "Directcast".  That is the equivalent of the VB CType().

For C#, the code would be:

TextBox t = FormView1.FindControl("DataTextBox") as Textbox;

The "as" operator differs from a standard cast in that it returns null if the cast fails and is limited to reference type casts (as is the case with VB DirectCast).


Friday, October 12, 2012 1:18 AM

If you want to revive an old thread you should check your facts.

DirectCast does not produce 'Nothing' if the cast fails - it produces an exception - this is nothing at all like the "as" operator in C#.  What you probably mean is that "as" is the C# equivalent to VB's "TryCast".

Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter


Friday, October 12, 2012 2:47 AM

The "as" operator in C# is more like DirectCast in VB in that the operation is more direct and can result in faster performance. The "as" operator in C# only works with reference types as is the case with DirectCast in VB.

I dd point out that the "as" operator differs in that it won't throw an exception.


Friday, October 12, 2012 2:53 AM

The exact equivalent of "as" in VB is "TryCast" (which also only works with reference types).

The C# "as" operator is not especially direct compared with regular C# casts and certainly not faster.

VB's DirectCast was introduced as a closer equivalent to regular C# casts than CType, and of course is faster when it's allowed.

Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter


Friday, October 12, 2012 3:05 AM

That's entirely incorrect.  TryCast will not throw an exception but DirectCast is faster as is "as" in C#.

With the exception (pardon the punn) of throwing an exception, the C# "as" operator is most directly comparable with the VB DirectCast.

C# prefix-cast: works with value and reference types as does VB CType

C# prefix-cast throws an exception if the cast fails as does VB CType

C# "as" cast only works with ref types as does VB DirectCast

C# "as" cast is faster than prefix-cast as is VB DirectCast

VB CType relies on VB-specific casting mechanisms, not the native .NET casting features. 

VB DirectCast performs better than CType as C# "as" performs better than prefix-casting as they both (DirectCast and "as") follow the same runtime casting procedure.

Why C# "as" is faster than prefix-casting:

http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/

Why VB DirectCast is faster than CType or TryCast

http://msdn.microsoft.com/en-us/library/7k6y2h6x(v=vs.71).aspx


Friday, October 12, 2012 3:47 AM

Not quite entirely incorrect.

"TryCast" in VB is identical in operation and performance to C#'s "as" operator.

"DirectCast" in VB is faster than TryCast or CType in the same way that C# prefix casting is faster than "as".

See the following discussion - the article you quote is misleading:

http://stackoverflow.com/questions/2139798/why-is-the-c-sharp-as-operator-so-popular

To summarize:

VB TryCast is identical to the C# 'as' operator.

VB DirectCast is close to C# prefix casting, but different in that it only operates on reference types.

The closest thing you have in C# to CType are the System.Convert methods for a few common .NET types and prefix casting for other types.

Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter