Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Is 
2.1.
Example Code
2.1.1.
Output
3.
As keyword
3.1.
Example Code
3.1.1.
Output
4.
Is vs. As
5.
Frequently Asked Questions
5.1.
What is the purpose of the var data type in C#??
5.2.
What should we do if we want the Tuple to include more than eight elements?
5.3.
When should I use C# HashSet method?
5.4.
What is a keyword?
5.5.
What is a delegate?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Is and As Keyword in C#

Author Saksham Gupta
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Millions of developers use C#. It is a trendy programming language. Today we will see essential keywords, i.e., Is and As keywords in C#. 'as' and 'is' are the keywords used for conversion/TypeCasting in C#

Is 

The 'is' keyword determines if a conversion from one object type to another is compatible. If the conversion is compatible, it returns yes; otherwise, it returns false.

Example Code

// C# program to illustrate the
// use of is operator keyword
using System;


// taking a class
public class coding { }


// taking a class
// derived from P
public class codingNinja : coding { }


// taking a class
public class Coding Ninjas Studio { }


// Driver Class
public class code {


// Main Method
public static void Main()
{
coding o1 = new coding();
codingNinja o2 = new codingNinja();
Console.WriteLine(o1 is coding);
Console.WriteLine(o1 is Object);
Console.WriteLine(o2 is codingNinja);
Console.WriteLine(o2 is Object);
Console.WriteLine(o2 is codingNinja);
Console.WriteLine(o1 is Coding Ninjas Studio);
Console.WriteLine(o2 is Coding Ninjas Studio);
}
}

Output

True
True
True
True
True
False
False
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass