Showing posts with label casting. Show all posts
Showing posts with label casting. Show all posts

Thursday, September 4, 2008

[.NET] C# 'is' operator

The C# 'is' operator looks like a boring little fella at first, but once you get to know it you will eventually appreciate it - it checks if you can safely cast an instance of an object to a type:
///////////////////////////////////////
//example 1
string testStr = "";
object obj = testStr;

//..somewhere else in your code
string newStr;

if (obj is String)
{
   newStr = (string)obj;
   //go nuts with your string
}
///////////////////////////////////////


It is important to understand that since the 'is' operator is just checking if it is safe to cast the following expression will always be true for any type:
///////////////////////////////////////
//example 2
string var = "whatever";

if(var is object) //<-always true (for var of any type)
{
    //always executed 
}
 /////////////////////////////////////// 
The above is always true for any type because everything in .NET inherits from object. This means you can safely cast to object (box) value types for example, and even if quite obvious it helps understanding how the 'is' operator works. 

You can obtain a result similar to example 1 using GetType and typeof if you're resolving types down to an inheritance line with this code: 
/////////////////////////////////////// 
//example 3 
string testStr = ""; 
object obj = testStr; 

//..somewhere else in your code 
string newStr; 

if (obj.GetType().Equals(typeof(String))) 
   newStr = (string)obj; 
   //have fun with your string 
/////////////////////////////////////// 
But here you're just checking for equality and not for safe casting in general, as you do with operator 'is' (i.e. example 2 with GetType Equals typeof returns false - because GetType returns string does not equal object). For an insight on GetType and typeof see this post.

Closing - a probably more efficient way - depending on your taste - to check if casting is safe is to go ahead and cast using the operator 'as', like this:
///////////////////////////////////////
//example 4
MyClass test =  new MyClass("read between the lines");
object obj = test;

//..somewhere else in your code
MyClass newTest = (obj as MyClass);

if (newStr != null)
{
   //do CRAZY stuff with YourClass
}
///////////////////////////////////////

For more details about the 'as' operator have a look right here.

kick it on DotNetKicks.com

Monday, September 1, 2008

[.NET] Difference between GetType and typeof

GetType and typeof come in useful when you need to mess with objects types before casting and so forth.

The difference between the two is the following:
GetType is a method of the object class - from which everything inherits in .NET - while typeof is an expression that operates on a type (same as you declare variables with).
While GetType takes into account inheritance and gives you back the actual Type of your instance at runtime - typeof just resolves the type into a System.Type object (jeez) at compiletime.

To cut a long story short: GetType extracts the Type from the object - typeof extracts the correspondent System.Type object from a type (declaration).


Have a look @ an example and it will be clearer:
////////////////////////////////////////////////
Type myType;
//the following blocks of code are equivalent
{
myType = typeof(int);
}

{
int i;
myType = i.GetType();
}
/////////////////////////////////////////////////

Good stuff - ain't it? No, it ain't - this post is child of absolute boredom.

kick it on DotNetKicks.com

Friday, August 29, 2008

[.NET] Difference between casting and 'as' operator in C#

This is an easy one - but it can be useful to someone so here it comes:

The 'as' operator in C# is a tentative cast - if it's impossible to cast (types are not compatible) instead of throwing an exception as the direct cast the 'as' operator sets the reference to null.

So you can have something like:

MyClass myObj = new MyClass();
MyOtherClass myOtherObj = myObj as MyOtherClass;
if (obj != null)
{
//cast was successful
}
else
{
//a little bit of a fuck-up
}

The correspondent in VB is the TryCast (usage is the same as DirectCast but it doesn't throw any exception).