As keyword
The term 'as' is used to convert from one type to another. The type can be nullable or reference. The 'as' keyword determines whether one object type is compatible with another. If the new object type is compatible, it will return its value; otherwise, null will be returned.
If the conversion from one type to another fails, a null value will be returned instead of an exception being raised. As a result, the return value can potentially be null.
Example Code
// C# program to illustrate the
// concept of 'as' operator
using System;
// Classes
class coding { }
class ninja { }
class Coding Ninjas Studio {
// Main method
static void Main()
{
// creating and initializing object array
object[] orr = new object[5];
orr[0] = new coding();
orr[1] = new ninja();
orr[2] = "Coding Ninjas Studio";
orr[3] = 6880;
orr[4] = null;
for (int q = 0; q < orr.Length; ++q) {
// using as operator
string s = orr[q] as string;
Console.Write("{0}:", q);
// checking for the result
if (s != null) {
Console.WriteLine("'" + s + "'");
}
else {
Console.WriteLine("Not string");
}
}
}
}
Output
0:Not string
1:Not string
2:'Coding Ninjas Studio'
3:Not string
4:Not strin
Is vs. As
The is operator determines whether an object's run-time type is compatible with the provided type, whereas the as operator converts between compatible reference types or Nullable types.
The is operator has a boolean type, whereas the as operator does not.
The is operator returns true if the provided object and the given type are of the same type, whereas the as operator returns the object when the two types are compatible.
If the provided object is not of the same type, the is operator returns false, whereas the as operator returns null if the conversion is not possible.
The is operator is only used for reference, boxing, and unpacking conversions, while the as operator is only used for nullable, reference, and boxing conversions.
Frequently Asked Questions
What is the purpose of the var data type in C#??
Keywords are keywords used in a language to denote predetermined activities or internal processes. The term var is used to declare an implicit type variable, which depends on its initial value to determine its type.
What should we do if we want the Tuple to include more than eight elements?
Only eight elements are allowed in the Tuple. You'll need to utilize nested tuples if you need to hold additional elements. However, this may result in uncertainty.
When should I use C# HashSet method?
HashSet is commonly used for storing unique data and performing high-performance computations. When you want your data to be unique and avoid duplicate values, you can use a HashSet, which speeds up the element searching process.
What is a keyword?
Keywords are reserved words that have a special meaning for the compiler. These terms can't be used as identifiers for variables, classes, or anything else. As a result, there are numerous categories into which keywords might be classified. Let's take a look at them!
What is a delegate?
A delegate is a type that represents methods that have a set of arguments and a defined return type. When we instantiate a delegate, we may attach it to any method with an appropriate signature and return type. The delegate instance is used to call the method.
Also Read About - singleton design pattern in c#
Conclusion
In this article, we discussed it is and as a keyword in C#. To learn more about C#, visit our page, C#. I hope you would have gained a better understanding of these topics now!
Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more?
Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!