473,799 Members | 3,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is null allowed in C#?

Hi,

Is null value for a class instance allowed in C#? for example:
MyClass obj = null;

If Not, how to initiate an instance with value 0? How to detect if an
instance is "newed" ? And how to pass a null value to a class constructor
whch has a object paramer can be 0?

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

Thanks
Nov 16 '05 #1
2 2082
Hi there...

In your first sample ur definining an object (variable) but not creating it.
You know it's "newed" cuz obj != null
Yes it's possible to pass null to a constructor... for example

using System;

class Test {
static void Main() {
MyClass x = new MyClass(null);
Console.WriteLi ne("MyClass::Me mber is " + ((object) (x.Member !=
null ? "Not null" : "Null")).ToStri ng());
Console.ReadLin e();
}
}
class MyClass {
private object m_member;

public object Member {
get {return m_member;}
}

public MyClass(object arg) {
m_member = arg;
}
}
Regards,
--
Angel J. Hernández M.
MCSD
"Fei Li" <Fe***@discussi ons.microsoft.c om> escribió en el mensaje
news:DC******** *************** ***********@mic rosoft.com...
Hi,

Is null value for a class instance allowed in C#? for example:
MyClass obj = null;

If Not, how to initiate an instance with value 0? How to detect if an
instance is "newed" ? And how to pass a null value to a class constructor
whch has a object paramer can be 0?

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

Thanks

Nov 16 '05 #2
Fei Li <Fe***@discussi ons.microsoft.c om> 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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

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

Similar topics

26
45453
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
14
2709
by: William Ahern | last post by:
is this legal: FILE *func(void) { extern int errno; return (errno = EINVAL, NULL); } my compiler (gcc 2.9.x on openbsd) is complaining that i'm returning a pointer from int w/o a cast. gcc 3.x on linux, however, never made a peep
24
20103
by: RHNewBie | last post by:
Hello, What is the difference between null and NULL. Are x == null and x == NULL the same? The compiler is gcc 3.2. Thanks.
3
7662
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
102
6072
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" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
64
3957
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? - AFAIK C allows "null pointers" to be represented differently then "all bits 0". Is this correct? - AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work just like `void* p=NULL'. Is this correct?
51
4244
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember some instances where i did just that and printf reported something like : (null). I'm asking because i read somewhere that it is not valid. I thought it simply wasn't valid to dereference a pointer who is _not_ set to point to anything.
7
4915
by: Lutz Richter | last post by:
Hi, given the following code: ----------------------------- class B { public: B(): Value(99) {} int Get() { if (this) return Value; else return -1; }
18
3275
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
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
9077
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...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
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();...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
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
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.