Re: is null allowed in C#?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 08/25/04


Date: Wed, 25 Aug 2004 07:09:02 +0100

Fei Li <FeiLi@discussions.microsoft.com> wrote:
> Is null value for a class instance allowed in C#? for example:
> MyClass obj = null;

obj is a variable, not a class instance. null is effectively something
saying, "This value isn't a reference to a class instance at all. It's
a reference to no object."
 
> If Not, how to initiate an instance with value 0?

What do you mean "with value 0"?

> How to detect if an instance is "newed" ?

If a reference is non-null, it's a reference to an instance.

> And how to pass a null value to a class constructor
> whch has a object paramer can be 0?

Just pass null.

> In general, how C# handle a pointer with value 0;

null is a special reference. You can possibly think of it as a pointer
with value 0, but it's better not to think in those terms - just think
of it as a reference meaning "No object".

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Q: global variable
    ... The life span of a variable is as long as the class instance it is ... We typically don't hold a reference to a Page ... A static field ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: "Global" objects
    ... reference to a class instance. ... Static objects are problematic, and should be ... protected to a class instance that they reside in (other than the Form ... have a main form on which I show various user controls depending on ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: An object-oriented network DBMS from relational DBMS point of view
    ... The sentence has a context, it presumes a memory model, you can point ... "A class instance is explicitly created by a class instance creation ... In the context of the Java memory model, you need a reference to them ...
    (comp.databases.theory)
  • Re: How to Clone a class instance
    ... > Could anyone show me how to clone a class instance without writing code to ... contained objects), with some of them reference counted, blind memory ... instance1 is already a typed pointer (usually referred to as a "reference") ...
    (borland.public.delphi.language.objectpascal)
  • Re: Am I the ONLY person that didnt know this?
    ... When an class instance is not locally created, the VMT must be retrieved ... if cond then obj:= TFileStream.Create ...
    (comp.lang.pascal.delphi.misc)