Click here to Skip to main content
15,069,297 members
Articles / Programming Languages / Visual Basic
Posted 22 Sep 2003


94 bookmarked

Cheat Sheet - Casting in VB.NET and C#

Rate me:
Please Sign up or sign in to vote.
4.53/5 (72 votes)
22 Sep 2003Public Domain2 min read
Describes several casting and type related operations in VB.NET and C#.


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 behaviour 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.

  2. 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.

  3. Testing if an object is of a particular type, can be done with the TypeOf...Is operator:

    If TypeOf obj Is TextBox Then...
  4. Obtaining a System.Type object for a given type can be done with the GetType operator:

    Dim t As System.Type
    t = GetType(String)
  5. Obtaining a System.Type object for a given object can be done with the GetType method:

    Dim t as System.Type
    t = obj.GetType()

Casting in C#

  1. C# is a strictly typed language. Whenever types don't match, casting is necessary.

    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;
  2. 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;
  3. Testing if an object is of a particular type, can be done with the is operator:

    if (obj is TextBox) {...}
  4. Obtaining a System.Type object for a given type can be done with the typeof operator:

    System.Type t;
    t = typeof(String);
  5. Obtaining a System.Type object for a given object can be done with the GetType method:

    System.Type t;
    t = obj.GetType();


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Rudi Breedenraedt
Architect RealDolmen
Belgium Belgium
Rudi is a Software Architect at RealDolmen.

Comments and Discussions

GeneralMy vote of 5 Pin
Sibeesh Venu8-Aug-14 6:02
professionalSibeesh Venu8-Aug-14 6:02 
GeneralMy vote of 5 Pin
Raja Sekhar S3-Jun-13 19:05
MemberRaja Sekhar S3-Jun-13 19:05 
GeneralMy vote of 5 Pin
Global Analyser18-Mar-13 3:11
MemberGlobal Analyser18-Mar-13 3:11 
GeneralMy vote of 4 Pin
Luis Gustavo Fernández Batres10-Mar-13 14:31
professionalLuis Gustavo Fernández Batres10-Mar-13 14:31 
GeneralMy vote of 5 Pin
JGeZau22-Oct-12 8:19
MemberJGeZau22-Oct-12 8:19 
GeneralMy vote of 5 Pin
Mcwatt775-Oct-10 14:51
MemberMcwatt775-Oct-10 14:51 
GeneralMy vote of 4 Pin
richardhenwood13-Jul-10 22:30
Memberrichardhenwood13-Jul-10 22:30 
GeneralHi Pin
manchisrikanth10-Jun-10 2:22
Membermanchisrikanth10-Jun-10 2:22 
GeneralA note about as Pin
Wjousts24-Jul-09 4:16
MemberWjousts24-Jul-09 4:16 
GeneralConverting Variables Pin
SashoJ29-Jun-09 7:32
MemberSashoJ29-Jun-09 7:32 
GeneralRe: Converting Variables Pin
Wjousts24-Jul-09 4:15
MemberWjousts24-Jul-09 4:15 
GeneralRe: Converting Variables Pin
Southmountain1-Jul-19 8:47
MemberSouthmountain1-Jul-19 8:47 
GeneralMissing a lot Pin
JoshMouch25-Feb-07 14:27
MemberJoshMouch25-Feb-07 14:27 
Generaldifference VB/C# in compile-time vs run-time error Pin
Alexis Rzewski7-Feb-07 4:51
MemberAlexis Rzewski7-Feb-07 4:51 
QuestionHow about casting arrays Pin
Eric P Schneider19-Mar-05 8:57
MemberEric P Schneider19-Mar-05 8:57 
AnswerRe: How about casting arrays Pin
collinsauve7-Aug-08 3:28
Membercollinsauve7-Aug-08 3:28 
GeneralToString Pin
George_Seremetidis28-Sep-04 13:40
MemberGeorge_Seremetidis28-Sep-04 13:40 
GeneralDon't Forget System.Convert.ChangeType Pin
Shaun Hayward29-Jun-04 7:35
MemberShaun Hayward29-Jun-04 7:35 
GeneralRe: Don't Forget System.Convert.ChangeType Pin
skjagini6-Sep-05 2:56
Memberskjagini6-Sep-05 2:56 
JokeRe: Don't Forget System.Convert.ChangeType Pin
Artur M.7-Dec-06 21:45
MemberArtur M.7-Dec-06 21:45 
GeneralProblems with casting objects Pin
simbakid3-Mar-04 3:04
Membersimbakid3-Mar-04 3:04 
QuestionDifference between DirectCast and Ctype? Pin
FruitBatInShades26-Jan-04 5:36
MemberFruitBatInShades26-Jan-04 5:36 
GeneralPlease add pointers to the next update! Pin
Anonymous4-Oct-03 13:20
MemberAnonymous4-Oct-03 13:20 
GeneralOK, but could be better Pin
David Bennett30-Sep-03 4:23
MemberDavid Bennett30-Sep-03 4:23 
GeneralInformative Pin
Nish Nishant23-Sep-03 20:14
sitebuilderNish Nishant23-Sep-03 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.