TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Is: Cast Example

This C# article shows how to use the is-cast. This cast helps with testing types: it returns true or false.

Is, a cast, returns true if it succeeds. It returns false if the cast fails. With it, you cannot capture the converted variable. This operator is most useful when checking types in if-statements and expressions.

Note: The is-cast is only ideal if the resulting variable will not be needed for further use.

So: If you need to use the cast variable, use the as-cast construct instead, which provides further features.

As

Example. We use "is" in an if-statement. Like any operator, the is-operator is used to create an expression. It returns a boolean result of true or false. This is compatible with the if-statement, which requires a boolean context.

Next: The is-operator evaluates the entire class derivation chain when it is applied. The string is both an object and string.

C# program that uses is-operator to cast

using System;
using System.Text;

class Program
{
    static void Main()
    {
	// Create a string variable and cast it to an object.
	string value1 = "Example";
	object value2 = value1;

	// Apply the is-operator with three different parameters.
	if (value2 is object)
	{
	    Console.WriteLine("is object");
	}
	if (value2 is string)
	{
	    Console.WriteLine("is string");
	}
	if (value2 is StringBuilder)
	{
	    Console.WriteLine("is StringBuilder"); // Not reached
	}
    }
}

Output

is object
is string

We see three different is-casts being performed in the if-statement expressions. The first two if-statement bodies are reached. Their expressions are evaluated to the true value: the casts succeed.

Note: The third if-statement body is not fully evaluated because the string is not a StringBuilder type.

StringsStringBuilder

Instructions. At the level of the intermediate language, the is-operator cast is implemented with the isinst instruction. This instruction checks the very top of the evaluation stack and compares the type of that variable to the argument type.

IL

Performance. Often, you may need to actually use in some way a cast value. The is-cast only functions as a test, as it only returns a boolean. To store the value received by a cast in a local variable, please use the as-operator cast.

Tip: As returns null if the cast does not succeed. The FxCop static analysis tool provides tips on this.

FxCop

Summary. There are other ways to cast types in the language. But the is-operator is exception-neutral. It is elegantly used in an if-statement expression—it is evaluated to a boolean value.

Finally: We examined the instruction-level code in short and pointed to the as-cast.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf