Click here to Skip to main content
15,914,396 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
'as'which is used to check only for ref type this is ok,
i was msdn found answered , i dont found advantage, kindly revert me.
i dont found the advantages of 'as' key word.
Posted
Comments
Timberbird 18-May-12 7:49am    
You found no advantage compared to explicit cast? What about exception cast produces while "as" doesn't? I believe MSDN mentions it clearly. Or maybe you're talking about checking with "is" keyword?
preet88 18-May-12 7:51am    
i hope you have looked into this as well
http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
check this post too
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=21
hope you'll get answer to your queries.
Thanks

The as operator is used to perform certain types of conversions between compatible reference or nullable types.

The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception. Consider the following expression:
C#
expression as type

It is equivalent to the following expression except that expression is evaluated only one time.
C#
expression is type ? (type)expression : (type)null

Note that the as operator only performs conversions to reference or nullable types, and boxing conversions.
The as operator cannot perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.

Please refer: as Keyword[^]
Benefits of using “as” in C#[^]
 
Share this answer
 
v2
Comments
VJ Reddy 18-May-12 8:03am    
Good answer. 5!
Prasad_Kulkarni 18-May-12 8:11am    
Thank you VJ!
BillW33 18-May-12 9:11am    
Very good answer, 5. :)
Prasad_Kulkarni 18-May-12 9:12am    
Thank you CIDev!
Wendelius 18-May-12 15:54pm    
Good answer
There are two safe ways to check if an object is of the type you expect: as and is. The third way is to use a cast and see it it throws an exception, but that is a bad practice and should be avoided.

The difference is that is just check, and returns true or false, while as converts, and returns null if it isn't a type in the object inheritance.

So you can do it two ways:
C#
if (obj is myType)
   {
   MyType mt = (MyTpe) obj;
   ...
   }
Or
C#
MyType mt = obj as MyType;
if (mt != null)
   {
   ...
   }
I prefer the second as it includes an explicit null check.

We can use this in common routines, for example a Click event handler that could come from a variety of sources:
C#
private void button1_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        ...
        }
    }
If it isn't a button, then we may not know what to do with it!
 
Share this answer
 
Comments
VJ Reddy 18-May-12 8:08am    
Good answer. 5!
[no name] 18-May-12 9:06am    
I wonder if it's really necessary for a button click function to check if
the sender equals null. What do you think?
BillW33 18-May-12 9:17am    
The button1_Click method is just another method and it is possible for it to be called from anywhere, not just from a button’s Click event. That may not be a good practice, but I have seen bizarre things done. Checking before converting sender to a Button type provides protection against bizarre things at very little cost.
OriginalGriff 18-May-12 9:45am    
Very true - it also doesn't have to be the handler for just a Button click - any matching signature event handler can use it as well.
(It was called button1_Click because that is the VS default and I like to check my code compiles before I post it here :laugh:)
BillW33 18-May-12 9:18am    
Oh, and I also think this is a 5 worthy answer. :)
The as operator is used to perform conversions between types that are compatible. Used in an expression with the form:
C#
expression as type

The expression is a reference type and the type is a reference type

More on This link
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900