as keyword
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
as keyword
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  abstract keyword [next Lesson]  while keyword
as keyword
  by: Salman Ahmed

as

The as operator is used to perform conversions between types that are compatible. Used in an expression with the form:

expression as type


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

Key points

The as operator is similiar to a cast but on any conversion failure null is returned as oppose to raising an exception. Basically it is similiar to the expression:

expression is type? (type)expression : (type)null
Example:
// keyword as

using System;

class CSharpFriends
{
    // empty
}

class ASPDotNet
{
    // empty
}

public class Test
{
    public static void Main()
    {
        object[] myObjects = new object[5];

        myObjects[0] = new CSharpFriends();
        myObjects[1] = new ASPDotNet();
        myObjects[2] = "C# is fun!";
        myObjects[3] = 2002;
        myObjects[4] = 99.99;    // the lowest price is the law!

        for(int i = 0; i < myObjects.Length; i++)
        {
            string s = myObjects[i] as string;
            Console.Write("myObject[{i}]", i);

            if (s != null)
            {
                Console.WriteLine (" is ");
            }
            else
            {
                Console.WriteLine ("not a string");
            }

        }

        // pause console window
        Console.ReadLine();
    
    }

}
Output:

myObject[0] not a string

myObject[0] not a string

myObject[0] is a string!

myObject[0] not a string

myObject[0] not a string

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
as keyword
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  abstract keyword [next Lesson]  while keyword


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
sksandz 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center