473,398 Members | 2,404 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,398 software developers and data experts.

The is keyword

Hello!

I just want to check with you the usage of the is keyword is only relevant
when having reference type.

Is that correct?

//Tony
Jul 31 '08 #1
6 1466
On Jul 31, 4:51*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
I just want to check with you the usage of the is keyword is only relevant
when having reference type.

Is that correct?
Well, the expression on the left has to be a reference type, but the
type name on the right could be a value type when it comes to boxing:

object o = 10;
if (o is int)
{
// Yup
}

Jon
Jul 31 '08 #2
On Jul 31, 11:51*am, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Hello!

I just want to check with you the usage of the is keyword is only relevant
when having reference type.

Is that correct?

//Tony
the left side at least, you can do object1 is Int32.

You will fnid a different situation with the "as" operator, there the
second operator should be a reference type.
so this expression is valid:

int i = 23;
object e = i as object;
Jul 31 '08 #3
Well, the expression on the left has to be a reference type

It doesn't have to be... of course the compiler knows that structs can't
be subclassed, so will raise warnings about unnecessary usage, but
consider generics:

static int? CompareToDefault<T>(T value) where T : struct
{
if (value is IComparable<T>)
{
return Comparer<T>.Default.Compare(default(T), value);
}
else
{
return null;
}
}

The left hand operand is definitely a value-type...

Marc
Aug 1 '08 #4
On Aug 1, 10:11*am, Marc Gravell <marc.grav...@gmail.comwrote:
Well, the expression on the left has to be a reference type

It doesn't have to be... of course the compiler knows that structs can't
be subclassed, so will raise warnings about unnecessary usage, but
consider generics:
Ooh, very true. Interesting example :)

Can you think of any non-generic value type examples? I suspect
*those* are impossible, because other than generic cases, if the
compiler knows it's a value type, it knows the exact type. I could be
missing something though.

Jon
Aug 1 '08 #5
Well, you can still *use* is with known value-types; but the compiler
will ignore you ;-p But it is a warning, not an error...

But no, I can't think of any useful, non-generic value-type examples of
"is".

Marc
Aug 1 '08 #6
It happens that Ignacio Machin ( .NET/ C# MVP ) formulated :
On Jul 31, 11:51*am, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
>Hello!

I just want to check with you the usage of the is keyword is only relevant
when having reference type.

Is that correct?

//Tony

the left side at least, you can do object1 is Int32.

You will fnid a different situation with the "as" operator, there the
second operator should be a reference type.
so this expression is valid:

int i = 23;
object e = i as object;
Not quite, a nullable type will work as well. The main point is that
"null" should be an acceptable value for the receiving type.

object o = "nop";
int? i = o as int?; // i becomes null

or you could use
int i2 = o as int? ?? 2; // i2 becomes 2

Hans Kesting
Aug 1 '08 #7

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

Similar topics

5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
4
by: tzellman | last post by:
Ok, so here is my situation: Let's assume I have a function that makes good use of the kwargs parameter. It requires that there is a certain "format" for the kwargs keywords. (I am using Django,...
33
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a...
6
by: tom | last post by:
Hi I try to check whether a given input is keyword or not. However this script won't identify keyword input as a keyword. How should I modify it to make it work? #!usr/bin/env python import...
4
by: Pranjal9880 | last post by:
Hi all I am trying to parse the xml file using perl in which I am succeeded , I am able to fetch the data from the xml file by using one keyword. Now I want to do it using more than one keyword. It...
2
by: rlemusic | last post by:
Hi everybody, I’m creating a database in Access (I believe it’s 2000) to catalogue items in the archives of a small museum. I’m a total n00b as far as using Access goes, but by looking at some...
3
by: Redbeard | last post by:
Hi All this is my first time post, be gentle. I am looking at creating a keyword search that searches multiple fields in a Form and then filters records that match the keyword. The Form...
10
by: Armando Serrano Lombillo | last post by:
Why does Python give an error when I try to do this: Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> len(object=) TypeError: len() takes no keyword arguments but...
1
adelemb
by: adelemb | last post by:
Hi, I'm trying to make a SQL statement work and am getting quite mixed up with it, I hope someone can help! I have a form with a textbox named "keyword". I want the user to enter a keyword and...
1
by: alamodgal | last post by:
hiiiiiii I have a problem in highlighting searching keyword.Actually im using this function for searching Public Function HighLight(ByVal Keyword As String, ByVal ContentFor As String) Dim...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.