473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

paractical applications of the C# 'is' operator...

Hello,

I would like to see some practical uses of the C# is operator.
This operator is not present in C++ and I would like to know
what some good uses of it are... after all if I declare a
variable as int, I know that it is an int, so there is
no point to writing stuff like:

int x = 0;
if (x is int)
Console.WriteLine("x is an integer.");
Thank you for your feedback,

Neil
Nov 16 '05 #1
2 960

"Neil Zanella" <nz******@gmail.com> wrote in message
news:1a**************************@posting.google.c om...
Hello,

I would like to see some practical uses of the C# is operator.
This operator is not present in C++ and I would like to know
what some good uses of it are... after all if I declare a
variable as int, I know that it is an int, so there is
no point to writing stuff like:

int x = 0;
if (x is int)
Console.WriteLine("x is an integer.");


consider...

public void PrintObject(object o)
{
if (o is FinanceReport)
{
PrintFinanceReport((FinanceReport)o);
}
else if (o is EmployeeRecord)
{
PrintEmployeeRecord((EmployeeRecord)o);
}
}

(although you would often use 'as' or a table look up here for performance
reasons)

Its value is when the expression in question may hold a derived value
instead of a sealed type(like int is)
Nov 16 '05 #2
It's very useful when you start using polymorphism and interfaces.

Think about drag and drop. When an object is dropped on your control, you
want to see what that object "is", so you could write some code like:

if (dragItem is ToolBoxItem)
{
.ToolBoxItem toolBoxItem = (ToolBoxItem)dragItem;
...
}

Or perhaps you were iterating over objects in your form looking for objects
that support serialization?

foreach (Control ctl in Controls)
if (ctl is ISerializable)
...

You may know which controls support serialization seeing as you developed
the form, but sometimes it's better not to make assumptions because you may
change the form content later on.

It's particularly useful when you're provided with objects of which you're
not sure of the type of the object. Drag and drop, plug ins, or generic
routines you write that let you pass in a variety of object types.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"Neil Zanella" <nz******@gmail.com> wrote in message
news:1a**************************@posting.google.c om...
Hello,

I would like to see some practical uses of the C# is operator.
This operator is not present in C++ and I would like to know
what some good uses of it are... after all if I declare a
variable as int, I know that it is an int, so there is
no point to writing stuff like:

int x = 0;
if (x is int)
Console.WriteLine("x is an integer.");
Thank you for your feedback,

Neil

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
1
by: jayapal | last post by:
Hi , I am using the NEW operator to allocate the memory in many places of my code.But I am not doing any error hadling or exception handling.Can any one suggests me how to do exception handling,...
16
by: mail2sandeepnl | last post by:
hi all what are the applications / examples that requires concatenation of 2 pre-processor variables. i.e applications of " ## " operators in C/C++ i think its only use is in Compiler...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.