AS is in CSharp too.

Many CSharp programmers are surprised to find that CSharp has an AS keyword that looks remarkably similar to VB.NET’s AS keyword.  And while on the surface it looks similar to what AS does in VB.NET, it is quite a bit different.

To start with, you can’t declare a variable with the AS keyword.  But you can assign to a variable using the AS keyword, like so:

String s = "abc";
Object f = this;
s = f as String;

If you look at the code long enough, you’ll probably figure out that whatever object f is pointing to is being assigned to a string.  But the question you need to ask once you’ve figured this much out is what the difference is between this:

s = f as String;

and this:

s = (String)f;

and this:

s = Convert.ToString(f);

The answer is that the first two are casting operations, i.e., we are just moving pointers around and saying they are different types.  The last is a conversion operation.  No matter what type f actually is, you’ll create a brand new string object with the conversion.

That leaves us with trying to determine the difference between the first two.

Assuming you are familiar with the typical casting operation:

s = (String)f;

You’ll realize that if f is not actually pointing to a string, this operation will throw an exception at runtime.

You could test for the type of f prior to doing the conversion by using a ternary operator:

s = f is String ? (String)f : null;

so that you can avoid the exception and test s for null later on in your code.  Or you could use:

s = f as String;

which does the same thing.

That’s really all the AS keyword is for.  It’s just a shortcut syntax for a standard casting operation that avoids casting exceptions and returns null if the cast can’t happen.

Other Places Talking about casting in CSharp:

Using the as Statement to Cast Without Exceptions in C# : CSharp Feeds – Considering that plenty of times there will be variables passed using the object data type, it is important to be able to cast your variables into more usable types. Say for example you’re passed a variable as an object. …

Casting Syntax Vs AS Operator – Which Approach Is Better And Why … – The two most popular approaches to casting – converting data types and objects – in C# are Using the cast syntax ie (TypeToBeCastTo) (type/object)

Why you can’t cast an integer to a string. – To do a similar operation in CSharp, you’d actually use the functions in the Convert class. A cast, on the other hand, doesn’t change the variable. It just lets us access it as though it were another type. And you can only cast if the …

C# .NET What is Casting in C# – C-Sharp Guru replied to Arth Srivastava at 08-Sep-08 08:01. The best thing is to read http://msdn.microsoft.com/en-us/library/ms173105.aspx where all the types of casting has been given. Regards, …

C#: Casting with parentheses versus casting with as « Lxcid’s Weblog – Casting with as can only be done on reference type. On event of a conversion failure, casting with parentheses will raises an exception, while casting with as will yields a null. expression as type is equivalent to: expression is type ? …

Typecasting – C# – Typecasting. 1 Day Ago. Is there any way to type casting from NumberUpDown to string? AddThis Social Bookmark Button. gallian99. View Public Profile · Send a private message to gallian99 · Find all posts by gallian99 …

Oleg Sych – » The difference between as and () operators in C# – In C#, you have two different ways to perform type casting (a.k.a. explicit type conversion) – as and () operators. The difference between the two operators is not limited to just syntax, they work differently. …

This was first pointed out to me by Dan Mezick at New Technology solutions where he provides asp .net training as well as windows forms in both CSharp and VB.NET


Related Post

  • Why you can’t cast an integer to a string.Why you can’t cast an integer to a string. I saw this question last Thursday on Channel 9 that I've heard before. My guess is that there are enough people who have the same question that it's worth addressing. I know there's probably a re...
  • Delegates in .NETDelegates in .NET I received the following question: What is a delegate?  What problem does it solve?  and When might I use a delegate? A delegate is essentially a function pointer.  We have used function poi...
  • CSharp fixed keywordCSharp fixed keyword Since I've already mentioned my bias against using unsafe mode in this post: Advanced CSharp - unsafe mode I'll skip my normal rant about that.  Just suffice it to say they don't call it "uns...
  • Using VB.NET From CSharpUsing VB.NET From CSharp Back when I was teaching .NET to other programmers regularly, I would frequently get the following question from a VB6 programmer moving to CSharp. “Is there any way of using some of the VB funct...
  • CSharp adds the var keyword!CSharp adds the var keyword! There have been several new features added to the CSharp language that will significantly reduce the amount of code that ends up in our source files.  It will not significantly reduce the amount of...