as
operator does the down cast. Rather than throws exception out it assigns the reference to null.
using System;
class Person
{
public string name;
}
class Employee : Person
{
public string companyName;
}
class Program
{
static void Main(string[] args)
{
Person p = new Person();
Employee e = p as Employee;
}
}
After the as
operator we can use if statement to check the result.
using System;
class Person
{
public string name;
}
class Employee : Person
{
public string companyName;
}
class Program
{
static void Main(string[] args)
{
Person p = new Person();
Employee e = p as Employee;
if (e == null)
{
Console.WriteLine("successful");
}
}
}
The output:
successful
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |