

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use ‘as’ operator in C#?
The "as" operator perform conversions between compatible types. It is like a cast operation and it performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.
The following is an example showing the usage of as operation in C#. Here ‘as’ is used for conversion:
string s = obj[i] as string;
Try to run the following code to work with ‘as’ operator in C# −
Example
using System; public class Demo { public static void Main() { object[] obj = new object[2]; obj[0] = "jack"; obj[1] = 32; for (int i = 0; i < obj.Length; ++i) { string s = obj[i] as string; Console.Write("{0}: ", i); if (s != null) Console.WriteLine("'" + s + "'"); else Console.WriteLine("This is not a string!"); } Console.ReadKey(); } }
Output
0: 'jack' 1: This is not a string!
- Related Questions & Answers
- How to use Operator Overloading in C#?
- How to use *= operator in jQuery attribute selector?
- How to use the instanceof operator in Java?
- How to use Null Coalescing Operator (??) in C#?
- How to use an assignment operator in C#?
- How to use the ?: conditional operator in C#?
- How to use INTERSECT operator in Android sqlite?
- How to use ‘is’ operator in C#?
- How to Use Images as Backgrounds in Tkinter?
- How to use Bitcoins as Traditional Currency?
- How to use the "in" operator in JavaScript?
- How to use comparison operator for numeric string in MySQL?
- How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?
- Java Program to use == operator to compare enums
- How to use arrow functions used as methods in JavaScript?
Advertisements