In this post I try to describe some commonly used C# operator and their VB equivalent. This is intended as quick reference for C# or VB programmers who know one language but are in  the process of learning the other. So, let's start.






typeof and GetType()




In C#, the typeof() operator is used for getting a reference to any type. For example,




Type t=typeof(string);




Will return a type object referring to string. The same result can be attained in VB by




Dim t As Type=GetType(String)







So, typeof in C# becomes GetType in VB.





As and TryCast


In C#, there exists a useful operator for converting between types without throwing errors. The as operator. The as operator only works on reference types. For example




TypeB b=new TypeB();




TypeA a=b as TypeA;




Note: TypeB inherits from TypeA. The as operator only supports inheritance and interface conversion.



The equivalent of above snippet in VB is,




Dim b As New TypeB




Dim a As TypeA = TryCast(b, TypeA)




 In both the example, if the conversion fails, the variable b will be null and no InvalidCast exception will be thrown.



 





Hope it helps,

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList