473,595 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'null' references

As explained in "Using pointers vs. references"
http://groups.google.ee/group/borlan...294c7b02e8faca ,
the pointers are allowed to be null, while references must refer an existing
variable of required type. The null is normally used for making optional
parameters. But there is no way to pass null reference in C#. Something is
missing.
Jun 15 '07 #1
76 4613
valentin tihomirov <V_*********@be st.eewrote:
As explained in "Using pointers vs. references"
http://groups.google.ee/group/borlan...ectpascal/brow
se_thread/thread/683c30f161fc1e9 c/ab294c7b02e8fac a#ab294c7b02e8f aca ,
the pointers are allowed to be null, while references must refer an existing
variable of required type. The null is normally used for making optional
parameters. But there is no way to pass null reference in C#. Something is
missing.
That's using Delphi terminology, which isn't terribly helpful in C#.

In C# null *is* a reference:

<quote>
A reference type value is a reference to an instance of the type, the
latter known as an object. The special value null is compatible with
all reference types and indicates the absence of an instance.
</quote>

and

<quote>
The null literal (§9.4.4.6) evaluates to the null value, which is used
to denote a reference not pointing at any object or array, or the
absence of a value. The null type has a single value, which is the null
value. Hence an expression whose type is the null type can evaluate
only to the null value. There is no way to explicitly write the null
type and, therefore, no way to use it in a declared type.
</quote>

Now, given that, I don't actually see what you're trying to do. You can
pass null as an argument without an problems. Try to describe it in
terms of achieving a goal, rather than being like another language.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 15 '07 #2
As explained in "Using pointers vs. references"
http://groups.google.ee/group/borlan...294c7b02e8faca ,
the pointers are allowed to be null, while references must refer an
existing variable of required type. The null is normally used for making
optional parameters. But there is no way to pass null reference in C#.
Something is missing.
Yes, for those coming from the C++ world such as myself it's an annoyance
(since you can't pass null for "out" or "ref" parameters that you're not
interested in). Other issues include a lack of "const-correctness", no
covariant return types (a must in OOP), the need to initialize all variables
all the time (even though it's not actually required on occasion - you still
have to do it though), and other pet peeves. "C#" certainly has benefits
that C++ can learn from but C++ still has much to teach as well. Part of the
problem with "C#" and .NET in general was its rush to get to market. This
likely resulted from the pressure MSFT was feeling to counter Java before it
was too late. It shows given how generics were introduced only in 2.0 for
instance (they should have been implemented from the outset) and many
classes are now obsolete or deprecated as result (for all intents and
purposes). .NET is therefore forced to carry this excess baggage around
including some ugly class derivations to support both the old interfaces and
their generic counterparts (or consider the even more insidious/pervasive
use of "object" which isn't type-safe and therefore a notorious source of
problems). Most of this will now be around permanently of course which shows
what happens when a language is pushed out the door too soon.
Jun 15 '07 #3
That's using Delphi terminology, which isn't terribly helpful in C#.
Now, given that, I don't actually see what you're trying to do. You can
pass null as an argument without an problems. Try to describe it in
terms of achieving a goal, rather than being like another language.
I do not understand why the terminology, which matches C very well is so
inconsistent with C#. Btw, which language do information technology
scientins base their theories on? Do they fix on some concrete language
explaining their discoveries? I just ask for some typical thing how to do it
in C# refering some other langs as examples.
In C# null *is* a reference:
This is great, but the following code fails to compile:
func(null);
as well as
func(ref null);
Jun 15 '07 #4
"valentin tihomirov" <V_*********@be st.eewrote in message
news:e$******** *****@TK2MSFTNG P02.phx.gbl...
This is great, but the following code fails to compile:
func(null);
as well as
func(ref null);
The first one does compile if the argument to func is a reference type:

void func(object x) {}
....
func(null); //OK

The second one requires a variable:

void func(ref object x) { if (x==null) x=new myclass(); }
....
object y = null;
func (ref y);
//now y contains a reference to a myclass object
Jun 15 '07 #5
object y = null;
func (ref y);
//now y contains a reference to a myclass object
A reference to the special NULL object is not the same thing as the
zero-pointer, which refers nothing (zero address is invalid). That is why I
point out that pointer argument itself is allowed to be zero while reference
is not. A reference must always refer something (whether a valid object or
'null' object).

Thanks for the hint. I suppose it is the supposed way to do it in C#. It has
two disadvantages though: 1) you must declare a bogus reference variable;
and 2) initialize it before passing to the function, where you could just
pass a 0 right away. Furthermore, it prevents you from using 'out'
arguments.

Actually, pointers are needed to write directly to the address they points
to. Using reference to the bogus null-object, which we leave unmodifed when
assign a new value to the reference to return. This makes passing the bogus
value by reference a bit nonsenes. Pointers reach the same effect without
creating any bogus objects, which as stated above has additional usage
advantages.

Peahaps, too much words for the issue. Yet, I do not know any places where
it is discussed.
Jun 15 '07 #6
On Fri, 15 Jun 2007 11:09:40 -0700, valentin tihomirov
<V_*********@be st.eewrote:
A reference to the special NULL object is not the same thing as the
zero-pointer, which refers nothing (zero address is invalid).
Frankly, I don't see how it's all that different at all. There are some
minor semantic differences, but for all intents and purposes a "null" in
C# is pretty much the same as a "null" in C++.
That is why I
point out that pointer argument itself is allowed to be zero while
reference
is not. A reference must always refer something (whether a valid object
or
'null' object).
You can't use a null for a reference parameter in C++ either. The
following generates a compile error, just as the "ref" parameter example
you gave for C# does:

void Func(int &i)
{
}

void Caller()
{
Func(nullptr);
}
Thanks for the hint. I suppose it is the supposed way to do it in C#. It
has
two disadvantages though: 1) you must declare a bogus reference variable;
and 2) initialize it before passing to the function, where you could just
pass a 0 right away. Furthermore, it prevents you from using 'out'
arguments.
Again, how is this difference from passing by reference in C++?
Actually, pointers are needed to write directly to the address they
points
to.
In C#, a reference is needed to write directly to the address it points
to. Same thing.
Using reference to the bogus null-object, which we leave unmodifed when
assign a new value to the reference to return.
I don't understand this statement. How can you leave something unmodified
while at the same time assigning a new value?
This makes passing the bogus
value by reference a bit nonsenes. Pointers reach the same effect without
creating any bogus objects, which as stated above has additional usage
advantages.
The only real difference I see is that if you pass a pointer to a
_pointer_ in C++, that allows you to have similar behavior to (but not
exactly the same as) passing by reference, while at the same time allowing
for the parameter to be optional. For example:

void Func(int *pi)
{
if (pi != null)
{
*pi = <some result goes here>;
}
}

void Caller()
{
int i;

Func(&i);
Func(null);
}

However, I fail to see how this is really much of an advantage. In C# you
would be required to create a dummy local that you would just ignore. In
C++ you have to have code in the function itself to check whether the
parameter is null. Either way, there's extra code, and in the C# case it
eliminates the chance of a null-dereference run-time error (in C++, if you
forget to check for the parameter being null, you can crash).

I see this as _better_, not worse.
Peahaps, too much words for the issue. Yet, I do not know any places
where
it is discussed.
Frankly, after having seen several of these "rants" from you, I am left
wondering why you bother to use C#. I don't really think you understand
the issues you're talking about anyway, but even assuming you do, it seems
to me that if you feel C++ is so much better suited to your needs, then
that's the language you ought to be using.

Pete
Jun 15 '07 #7
PS

"valentin tihomirov" <V_*********@be st.eewrote in message
news:Ob******** ******@TK2MSFTN GP04.phx.gbl...
>
> object y = null;
func (ref y);
//now y contains a reference to a myclass object

A reference to the special NULL object is not the same thing as the
zero-pointer, which refers nothing (zero address is invalid). That is why
I point out that pointer argument itself is allowed to be zero while
reference is not. A reference must always refer something (whether a valid
object or 'null' object).

Thanks for the hint. I suppose it is the supposed way to do it in C#. It
has two disadvantages though: 1) you must declare a bogus reference
variable; and 2) initialize it before passing to the function, where you
could just pass a 0 right away. Furthermore, it prevents you from using
'out' arguments.

Actually, pointers are needed to write directly to the address they points
to. Using reference to the bogus null-object, which we leave unmodifed
when assign a new value to the reference to return. This makes passing the
bogus value by reference a bit nonsenes. Pointers reach the same effect
without creating any bogus objects, which as stated above has additional
usage advantages.

Peahaps, too much words for the issue. Yet, I do not know any places where
it is discussed.
Personally I find these types of discussions to have null value.

PS
Jun 15 '07 #8
Frankly, I don't see how it's all that different at all. There are some
minor semantic differences, but for all intents and purposes a "null" in
C# is pretty much the same as a "null" in C++.
Despite you cannot pass the null as argument, which is ubequitous, the most
frequent parameter in C.
Again, how is this difference from passing by reference in C++?
Where do I tell that C# references are different from C++ references? I was
speaking about the difference between "pointers" and "references "!

Using reference to the bogus null-object, which we leave unmodifed when
assign a new value to the reference to return.
I don't understand this statement. How can you leave something unmodified
while at the same time assigning a new value?

Very easily.
function (ref a) {
if (a != null)
a = new object();
}

b = null;
function(ref b);

The null passed by reference is left unmodified ;-) The same effect would be
reached by different 'nulls'
NULL = new Object();
func(ref r) {
if (r != NULL)
r = returnVal;
}

However, I fail to see how this is really much of an advantage. In C# you
would be required to create a dummy local that you would just ignore. In
C++ you have to have code in the function itself to check whether the
parameter is null. Either way, there's extra code, and in the C# case it
eliminates the chance of a null-dereference run-time error (in C++, if you
forget to check for the parameter being null, you can crash).
... and the water is wet. But I did not address this issue! Read it once
again:
[quote] It has
two disadvantages though: 1) you must declare a bogus reference
variable;
and 2) initialize it before passing to the function, where you could
just
pass a 0 right away. Furthermore, it prevents you from using 'out'
arguments.[end quote]

Who argues that we should assign null (optional) arguments if the null
passed means "user does not want us to out anything"?

I don't really think you understand the issues you're talking about
anyway
Because you read and respond on the thing I do not address.
Jun 15 '07 #9
valentin tihomirov <V_*********@be st.eewrote:
object y = null;
func (ref y);
//now y contains a reference to a myclass object

A reference to the special NULL object is not the same thing as the
zero-pointer, which refers nothing (zero address is invalid). That is why I
point out that pointer argument itself is allowed to be zero while reference
is not. A reference must always refer something (whether a valid object or
'null' object).
Only according to your terminology. In C#, a reference can refer to a
real object or it can be null. There is no such thing as a "null
object".
Thanks for the hint. I suppose it is the supposed way to do it in C#. It has
two disadvantages though: 1) you must declare a bogus reference variable;
and 2) initialize it before passing to the function, where you could just
pass a 0 right away. Furthermore, it prevents you from using 'out'
arguments.
Yes, both "out" and "ref" require you to use an actual variable. This
is entirely reasonable given the purpose of ref, IMO.
Actually, pointers are needed to write directly to the address they points
to. Using reference to the bogus null-object, which we leave unmodifed when
assign a new value to the reference to return. This makes passing the bogus
value by reference a bit nonsenes. Pointers reach the same effect without
creating any bogus objects, which as stated above has additional usage
advantages.
No objects are created in the code at the top of this post.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 15 '07 #10

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

Similar topics

10
42395
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed to be optional, it's very definition is it's a necessary link to the parent table and part of the definition. If it's optional it shouldn't be part of the definition of a table and should be in a linking table instead. Comments?
99
5112
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
10
2591
by: Jason Collins | last post by:
There are some Requests that occur that have a NULL UserAgent. Some of these are legitimate crawlers (e.g., Overture's crawler), and I'm attempting to adjust my <browserCaps> to correctly classify crawlers. In an attempt to capture browsers with (UserAgent == null), I've added a case like this: <filter> <case match="^$"> crawler=true
7
2207
by: teddysnips | last post by:
Table DDL below: The tables I have contain Timesheet information. Each row in the tblTSCollected table contains an entry for an employee into the timesheet system, specifically by scanning the barcode on their badge. A whole bunch of business logic periodically attempts to "pair" these into logically matched scans. For example, some employees will scan in and out of a single place of work. For these there will be a row written to...
3
1693
by: Vincent RICHOMME | last post by:
Hi, I would like to know how I can return a NULL reference. usually when I need to return an object and to know if the object is valid I use a pointer (if the object is nto valid I return NULL) : MyObj* MyClass::GetObj(const wxString& strObjName ) { ReaderIt it = m_scObjs.find( strObjName );
12
16746
by: Joe | last post by:
I might be overworked so please excuse this stupid question... Say I do the following: DataTable table = new DataTable(); myDataAdaptor.Fill(table); dataGrid1.DataSource = table;
27
4202
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
11
3240
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed in the constructor is destroyed. I have implemented a Dispose(), Dispose(bool), and ~Finalize...
8
2284
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 0xb7d4c2f7 in vfprintf () from /lib/tls/i686/cmov/libc.so.6 #2 0xb7d6441b in vsprintf () from /lib/tls/i686/cmov/libc.so.6 #3 0x08049ba0 in character_data::printf (this=0x800, argument=0x0) at character.c:198
0
7955
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7883
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8251
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6674
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5418
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.