Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 58
#ASP.NET 55
ASP.NET 50
#DOTNET 49
.NET 40
WCF 21
DOTNET 12
c# 8
windows azure 7
SILVERLIGHT 7

Archive · View All
April 2011 9
March 2011 9
February 2011 8
December 2010 7
November 2010 5
September 2010 5
August 2010 5
May 2011 4
October 2010 4
January 2011 2

Type casting with is and as operator in C#

Apr 15 2011 1:34AM by Dhananjay Kumar   

I have been seeing many developers are casting between two types using is operator. As we know, we do have as operator also for type casting.

For purpose of this article, we are going to use below two classes,

claas Player
{
    public string Name { get; set; }
    public string Team { get; set; }
}
class WicktKeeper : Player
{
    public int NumberOfCatch { get; set; }
}

Very first let us try to understand, how is operator works?

So , if we are running below code , we will get output as true because ofccouse p is a Player.

Now let us modify code a bit and check whether p is compatible to Math class or not? Since p is instance of class Player and Player class does not have Math class in hierarchy tree, so output we will get false.

If we compare an object against null, we will get always an output as false.

Normally we use is operator like below,

In above snippet, C# checks type compatibility twice and it costs the performance. So to simplify above code and improve performance C# gives us as operator to compare.

as operator find type compatibility, If an object is not compatible with given type then as operator returns null.

Tags: #DOTNET, #ASP.NET, ASP.NET, c#, BRH, .NET, Type Casting Operator,


Dhananjay Kumar
49 · 4% · 1198
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Type casting with is and as operator in C#" rated 5 out of 5 by 2 readers
Type casting with is and as operator in C# , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]