Illustration of Operator Keywords (as, is) with Examples in C# (C Sharp)

C# provides eight Operator Keywords where in each of them are used to perform varied activities. This article will introduce two operator keywords namely as and is. These keywords are illustrated with simple examples in this article.


Operator Keyword as:

This keyword is used to perform type conversions between reference types that are compatible with one another. Normally you do type conversions using casting. But when you perform a type conversion that is not valid, an exception will be thrown. Here is an example:

class testClass{
int prop;
}
class sampleClass {
public static void Main(){
testClass obj = new testClass();
String string1 = “Hello”;
String string2 = “Have a Nice Day”;
string1 = (String) string2;
string1 = (String)obj;
}
}

In this example, string2 is assigned to string1. Here casting is not required. Anyways it is legal and it works fine. You have also attempted to cast object of testClass as a string and assign it to string2. This is invalid and hence you will end up in an exception. What if you have an alternate solution to display null instead of throwing an exception? You can achieve it using as keyword. Here is the modified code of sampleClass using as keyword:

class sampleClass {
public static void Main(){
testClass obj = new testClass();
String string1 = “Hello”;
String string2 = “Hai”;
string1 = string2 as String;
Console.WriteLine(string1);
string2 = obj as String;
if(string2 == null) {
Console.WriteLine(“Invalid String Assignment”);
}
}
}

Output of this code will be:

Hai

Invalid String Assignment

In this case, the assignments are explicit and you might not really know the significance of as keyword. What if you have an array of Objects and you are trying to assign each element of the object array to a string? Elements of the array can either be an integer or a double or a string or even an instance of another class. Because all value types and reference types are part of Object. Now when you perform the assignment, at runtime it may end up in exceptions. But using as keyword, instead of throwing exceptions and halting the program it will assign the string value to null. Here is an example to illustrate this case:

class testClass{
int prop;
}
class sampleClass {
public static void Main(){
testClass testObj = new testClass();
object arr[] = new object[4];
arr[0] = 100;
arr[1] = 12.75;
arr[2] = “Good Luck”;
arr[3] = testObj;
for(int index=0; index<arr.Length; index++) {
string element = arr[index] as string;
if(element != null) {
Console.WriteLine(arr[index] + “is assigned as string”);
}
else {
Console.WriteLine(arr[index] + “cannot be assigned to a string”);
}
}
}
}

Output of this code will be:

100 cannot be assigned to a string
12.75 cannot be assigned to a string
Good Luck is assigned as string
testObj cannot be assigned to a string

Note that this keyword is used to perform type conversions only on reference types. User-defined conversions cannot be performed using this keyword.

Operator Keyword is:

In the above example, you directly assign an object as string and later if the string value is null then you come to know that it is an invalid assignment. What if you get an option to check if the object is string before proceeding with the assignment? You have this option using the operator keyword is. Modified version of sampleClass using is keyword is mentioned below:

class sampleClass {
public static void Main(){
testClass testObj = new testClass();
object arr[] = new object[4];
arr[0] = 100;
arr[1] = 12.75;
arr[2] = “Hello”;
arr[3] = testObj;
Console.WriteLine(“The String Values are:”);
for(int index=0; index<arr.Length; index++) {
if(arr[index] is string) {
string element = (string) arr[index];
Console.Write(arr[index] + “ “);
}
}
}
}

Output of this code will be:

The String Values are: Hello

There are certain restrictions when you are using is keyword in your code:

• You cannot overload this keyword

• You can use this keyword only for boxing/unboxing conversions and reference type conversions. You cannot use it for any user-defined conversions

• Left hand side of the is operator should be a named method or a valid type, it cannot be an anonymous method

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How does Yield Keyword of C# Use Lazy Evaluationm: Reference Types |How to Create Iterator to Process Generic List in C# | How to Use “add” and “remove” Contextual Keywords of C# | How to Use ForEach with Arrays in C# |Illustration of Operator Keywords (as, is) with Examples in C# | Illustration of Operator Keywords (new, sizeof) with Examples in C# |Illustration of Operator Keyword stackalloc with Examples in C# |Illustration of Operator Keywords (true, false) with Examples in C#|Illustration of Operator Keyword typeof with Examples in C# |List of Contextual Keywords in C#|


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.