Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 'is' operator Clarification

Tags:

c#

Does the is operator indicate whether or not an object is an instance of a certain class, or only if it can be casted to that class?

Assume I have a DbCommand called command that has actually has been initialized as a SqlCommand. What is the result of command is OracleCommand?

(SqlCommand and OracleCommand both inherit from DbCommand)

like image 867
theycallmemorty Avatar asked Jun 06 '11 14:06

theycallmemorty


People also ask

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why is C programming language important?

One of the most significant features of C language is its support for dynamic memory management (DMA). It means that you can utilize and manage the size of the data structure in C during runtime. C also provides several predefined functions to work with memory allocation.

Why is C called a mid level programming language?

C has the features of both assembly level languages i.e low-level languages and higher level languages. So that's why C is generally called as a middle-level Language. The user uses C language for writing an operating system and generates menu driven customer billing system.


1 Answers

It checks if the object is a member of that type, or a type that inherits from or implements the base type or interface. In a way, it does check if the object can be cast to said type.

command is OracleCommand returns false as it's an SqlCommand, not an OracleCommand. However, both command is SqlCommand and command is DbCommand will return true as it is a member of both of those types and can therefore be downcast or upcast to either respectively.

If you have three levels of inheritance, e.g. BaseClass, SubClass and SubSubClass, an object initialized as new SubClass() only returns true for is BaseClass and is SubClass. Although SubSubClass derives from both of these, the object itself is not an instance of it, so is SubSubClass returns false.

like image 56
BoltClock Avatar answered Oct 03 '22 01:10

BoltClock