Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Casting in VB.NET and C#.NET

Posted by Ravi Varma Thumati on May 15, 2009

Introduction

This article describes several casting and type related operations in VB.NET and C#.

Casting in VB.NET

  1. By default in VB, casting is automatically done for you when you assign objects to variables. The objects are then automatically casted to the variables’ type.

This behavior can be influenced by an option line on top of your code file:

Option Strict On
Option Strict Off

When on, casting is strict and not automatic.

  1. Explicit casting can be done with the cast operator CType() or DirectCast():
textbox = CType(obj, TextBox)
textbox = DirectCast(obj, TextBox)

The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType. DirectCast throws an InvalidCastException error if the argument types do not match.

  1. Testing if an object is of a particular type, can be done with the TypeOf...Is operator:
  2. Obtaining a System.Type object for a given type can be done with the GetType operator:
  3. Obtaining a System.Type object for a given object can be done with the GetType method:
  4. C# is a strictly typed language. Whenever types don’t match, casting is necessary.
If TypeOf obj Is TextBox Then...
Dim t As System.Type
t = GetType(String)
MessageBox.Show(t.FullName)
Dim t as System.Type
t = obj.GetType()
MessageBox.Show(t.FullName)

Casting in C#

Regular casting in C# follows the C(++) and Java syntax:

string s = (string)obj;

The casting operator applies to the complete chain on the right of it, so in the following example, not a, but a.b is casted to a Form:

Form f = (Form)a.b;

To cast parts of the chain, use brackets. In the following example, obj is casted to a Form:

string s = ((Form)obj).Text;
  1. C# knows an additional casting operator: as.

The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. In the following situation, btn gets the value null:

Object obj = new TextBox();
Button btn = obj as Button;
  1. Testing if an object is of a particular type, can be done with the is operator:
  2. Obtaining a System.Type object for a given type can be done with the typeof operator:
  3. Obtaining a System.Type object for a given object can be done with the GetType method:
if (obj is TextBox) {...}
System.Type t;
t = typeof(String);
MessageBox.Show(t.FullName);
System.Type t;
t = obj.GetType();
MessageBox.Show(t.FullName);
Advertisements

One Response to “Casting in VB.NET and C#.NET”

  1. I’ve truly discovered a lot reading this document. Undeniably high-quality information here. Blogposts including this make this blog worth coming back to for more information.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: