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;
s = (String)f;
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;
s = f as String;
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