as operator

In this chapter you will learn:

  1. What is as operator and how to use as operator
  2. How to use as operator with interface

Using as operator

as operator does the down cast. Rather than throws exception out it assigns the reference to null.

The general form:

expr as type

expr is the expression being cast to type. On succeed, a reference to type is returned. Otherwise, a null reference is returned.

using System;//from  j  av  a2s.c o m
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;// j  a v  a  2 s .co m
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:

as an interface

The following code shows how to use the as Keyword to Work with an Interface.

using System;/* ja v  a2 s. c om*/
   
public interface IPrintMessage
{
    void Print();
};
   
class Class1
{
    public void Print()
    {
        Console.WriteLine("Hello from Class1!");
    }
}
   
class Class2 : IPrintMessage
{
    public void Print()
    {
        Console.WriteLine("Hello from Class2!");
    }
}
   
class MainClass
{
    public static void Main()
    {
        PrintClass   PrintObject = new PrintClass();
   
        PrintObject.PrintMessages();
    }
}
   
class PrintClass
{
    public void PrintMessages()
    {
        Class1      Object1 = new Class1();
        Class2      Object2 = new Class2();
   
        PrintMessageFromObject(Object1);
        PrintMessageFromObject(Object2);
    }
   
    private void PrintMessageFromObject(object obj)
    {
        IPrintMessage PrintMessage;
   
        PrintMessage = obj as IPrintMessage;
        if(PrintMessage != null)
            PrintMessage.Print();
    }
}

Next chapter...

What you will learn in the next chapter:

  1. Get to know is operator
  2. How to use is operator with interface
  3. Use 'is' to avoid an invalid cast
  4. Runtime check with in keyword
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances