473,387 Members | 1,516 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,387 software developers and data experts.

Null comparisons in C# 2.0

Question: Will C# 2.0 offer a way to directly and reliably produce the
fastest MSIL null check, i.e. a single opcode?

The current C# compiler will emit this opcode automatically when
comparing a type against null that doesn't override operator==.
However, if the type does have its own equality operator that method
is called instead, which is of course much slower.

Since only a psychotic implementation of operator== would return a
different result for null checks than the default implementation, a
useful workaround to get fast code is to write ((object) obj == null).

Now I'm reading through the C# 2.0 specs and I see a problem. Inside a
template class or method, you can use (obj == null) if obj is of a
generic type. But obj might be a value type! The C# compiler will
support this special case but that means we can't use the (object)
cast anymore.

So what's the deal? Is there another way to check for null -- can we
perhaps call HasValue on arbitrary objects? Or is the C# compiler
going to be revised so that all "== null" and "!= null" expressions
are automatically mapped to the appropriate MSIL opcodes, even in the
presence of user-defined operators? That would be the best solution
IMO, as it would also simplify these operator methods.
--
http://www.kynosarges.de
Nov 16 '05 #1
2 1820
Christoph,
Now I'm reading through the C# 2.0 specs and I see a problem. Inside a
template class or method, you can use (obj == null) if obj is of a
generic type. But obj might be a value type!
Unless you restrict the type parameter to be a reference type only.
I'm not sure it makes sense to compare to null if you allow the type
parameter to be a value type.

Or is the C# compiler
going to be revised so that all "== null" and "!= null" expressions
are automatically mapped to the appropriate MSIL opcodes, even in the
presence of user-defined operators?


Overloaded operators can't currently be used with generic type
parameters, because there are no constraint to enforce that the
operators are defined. So == on a variable of a generic parameter type
should always compile to a brtrue instruction. If you have

class Foo {
public static bool operator ==(Foo f1, Foo f2) {...}
public static bool operator !=(Foo f1, Foo f2) {...}
}

class Generic<T> where T : class {
void Bar(T t) {
if ( t == null ) ...
}
}

and use Generic<Foo>, the operators in Foo will not be used by the Bar
method.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
On Wed, 14 Jul 2004 11:24:26 +0200, Mattias Sjögren
<ma********************@mvps.org> wrote:
Unless you restrict the type parameter to be a reference type only.
I'm not sure it makes sense to compare to null if you allow the type
parameter to be a value type.
That's a special clause for generics: comparing an instance of an
unconstrained generic type that is a value type to null always returns
false, rather than throwing an exception.

(See section 20.8.4, "Reference equality operators", in the current
draft of the C# 2.0 Specifications)
Overloaded operators can't currently be used with generic type
parameters, because there are no constraint to enforce that the
operators are defined. So == on a variable of a generic parameter type
should always compile to a brtrue instruction.


Hmm, I think the compiler should use an overloaded parameter if a base
class constraint is specified (see sections 20.7.2 "Member lookup on
type parameters" and 20.9.2 "Member lookup").

But now that I'm reading these sections I see that *without* such a
constraint, the System.Object implementation will be used, and that
should indeed compile to brtrue as you said.

So it looks like C# 2.0 already does exactly what I want. :-) Thanks!
--
http://www.kynosarges.de
Nov 16 '05 #3

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

Similar topics

36
by: John J | last post by:
The following code is complete and working within a class; however, it uses "e" as a pointer to an Entry object without checking if that it is not null. This of course doesn't matter if objects are...
2
by: Joel | last post by:
Hi, I added a field to my company table (PBV_rstCompany.Fields("Installer")) the default value of the field is Null. I place this If statement and it doesn't work If...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
9
by: G Fernandes | last post by:
Hello fellow C-goers, Comparisons of pointer variables to 0 are automatically converted to comparisons to NULL (which can be represented at the bit level but something non-zero). But how...
13
by: Federico Balbi | last post by:
Hi, I was wondering if PGSQL has a function similar to binary_checksum() of MS SQL Server 2000. It is pretty handy when it comes to compare rows of data instead of having to write long boolean...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
15
by: Tarun Mistry | last post by:
Hi guys, what is the best/correct way to check for a NULL object? I.e. myClass test; if(test == null) {}
6
by: Java script Dude | last post by:
Small glitch (IMHO) in JavaScript is related to isNaN() boolean function. If passed a null, it returns true which is incorrect. This one cause me quite a bit of grief until I detected it and was...
1
by: George Sakkis | last post by:
I'd like to extend the Null pattern from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68205 to handle special methods (e.g. Null, del Null, Null+1) but it's not always clear how to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.