As, the meaning of is? The using statement

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Is: Check whether the object is compatible with a given type. For example, the following code can determine an instance of MyObject, or whether the object from a type derivation of MyObject: 

if(obj is MyObject){}

If the expression provides a non empty, object and provided can be cast to the type and will not cause abnormal is expression, the calculation results will be true.

If the known expression is always true or is false, then the is keyword will result in a compile time warnings, but usually at run time calculation type compatibility.

Note: the is operator cannot be overloaded, is operator only consider a reference conversion, boxing and unboxing conversion. Don't consider other conversion, if the user defined conversion. With anonymous methods are not allowed in the Is operator on the left. Lambda expressions are the exception.

Object myObject = new Object();

Boolean b1 = (myObject is Object);


Boolean b2 = (myObject is Employee);


If the object reference is null, the is operator always returns false, because no object can check its type.

The is operator is often used as follows:

if (myObject is Employee)


Employee myEmployee = (Employee)myObject;


In this code, type CLR to actually check two objects. The is operator firstly check whether myObject is compatible with Employee type. If it is, then perform the conversion type within an IF statement, CLR will check again whether myObject references a Employee. CLR type checking increases the safety, but it has some effect on Performance. This is because the CLR must first determine the variables (myObject) the actual type of the object referenced. Then, CLR must traverse the inheritance hierarchy, with each base type to check the specified type(Employee). Because this is quite a common programming mode, so C# expertise to provide the as operator, is to simplify the writing code, while improving performance.

As: is used to check in compatible reference conversion between the type of performing certain types.

Employee myEmployee = myObject as Employee;

if (myEmployee != null)

{ }

In this code, CLR to verify whether myObject is compatible with Employee type; if it is, the as will return a non null to the same object.

Quote. If the myObject is not compatible with the Employee type, the as operator returns null.

Note: the as operator caused by type CLR only check one object. The if statement just check whether myEmployee is null. This check is faster than typing object more quickly.

The as operator working mode and type coercion, but it will never throw an exception. On the contrary, if the object cannot be converted, the result is null. So, the correct way is to check the reference is null. If the attempt to directly use reference final, a System.NullReferenceException exception is raised. The following code which is demonstrated:

Object o = new Object();

Create a new Object object.

Employee e = o as Employee;

The o into a Employee


Access to e will throw a NullReferenceException exception


The as operator is similar to a forced conversion operation. But cannot be converted, as returns null instead of an exception.


Expression as Type, which is equivalent to the following expression, but only one expression calculation.

expression is Type ?(Type)expression : (Type)null

Note: the as operator only performs reference conversions and boxing conversion. The as operator cannot perform other conversion, if the user defined conversion, this kind of transformation should use a cast expression to perform.
using Statement, define a generic delegate 

1 provided to ensure convenient syntax for correct use of IDisposable objects.

using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Users\Public\Documents\test.txt"))


string s = null;

while((s = sr.ReadLine()) != null)





It is with try blocks and finally blocks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ariel at November 16, 2013 - 11:16 AM