Braintique.com header
Left Navigation Bar

The as Operator

The as operator works like a cast, except that if it can’t perform the indicated conversion, it returns null (a null reference that does not refer to any object) rather than throwing an exception as an unsuccessful explicit conversion would.

For example, the following code snippet will store a null reference in the variable str (with the resulting display that “i is not a string”):

object i = 42;
string str = i as string;
if (str == null){
MessageBox.Show("i is not a string", "as demo")
}
else {
MessageBox.Show(str, "as demo");
}

null is not a string!

Had the variable i contained string data, such as

object i = "hello";

then the string data “hello” would have been displayed in the message box after the object type i was converted to the string type str.

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write