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

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 2063
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.WriteLine("MyClass::Member is " + ((object) (x.Member !=
null ? "Not null" : "Null")).ToString());
Console.ReadLine();
}
}
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***@discussions.microsoft.com> escribió en el mensaje
news:DC**********************************@microsof t.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***@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 - <sk***@pobox.com>
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
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.)...
14
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....
24
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
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
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"...
64
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")? -...
51
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...
7
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
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
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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...

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.