将引用的对象设置为null [英] Setting a referenced object to null

查看:208
本文介绍了将引用的对象设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会过度劳累所以请原谅这个愚蠢的问题...


说我做以下事情:


DataTable table = new DataTable();


myDataAdaptor.Fill(table);


dataGrid1.DataSource = table;


table = null;


为什么dataGrid1.DataSource仍然指向一个表?


如何将对象设置为null现在所有对该对象的引用

为空?


谢谢,

Joe

解决方案

Joe,

dataGrid1.DataSource = table;

table = null;

为什么dataGrid1.DataSource仍然指向一个表?


因为DataSource有自己对表的引用的副本。

如何将对象设置为null并且现在对该对象的所有引用
是null?




你不能。我坦率地说,我知道的语言可以做到这一点,除非

类专门用于使用某种间接引用,其中

它们引用了一个保持真实的对象参考。在这种情况下,

修改此代理中的引用。 class将切断对真实对象的所有

引用。

-


Stoitcho Goutsev(100)


"乔" < JB ******* @ noemail.noemail> écritdansle message de news:
Oz ******* *******@TK2MSFTNGP12.phx.gbl ...


|我可能会过度劳累所以请原谅这个愚蠢的问题......

|

|说我做以下事情:

|

| DataTable table = new DataTable();

|

| myDataAdaptor.Fill(table);

|

| dataGrid1.DataSource = table;

|

| table = null;

|

|为什么dataGrid1.DataSource仍然指向一个表?

|

|如何将对象设置为null并且现在具有对该对象的所有引用

|是空的吗?


你不能。你只能将对象的引用设置为null。


如果你知道C tthen你会记得对象引用持有对象的

地址你会有取消引用该指针以获得对象的
。你可以删除对象然后指针将是

无效并且必须被取消以避免AV。


在C#中,所有对象都是垃圾收集的,你可以't'删除他们。当

对给定对象的最后引用超出范围时,该对象可能会收取b
。你可以找到尽可能多的引用,但是

只要* any *引用仍然保持

对象,对象将保持活动状态。


在你的例子中,DataGris的DataSource持有对

表的引用,所以即使你使原始引用为null,''table'',对象
$'$ b''表''指向的是由DataSource属性保持活动状态。


为什么要将属性归零只分配了一个

的有效对象?


Joanna


-

Joanna Carter [TeamB]

顾问软件工程师





您的回答是我的答案思想。我只是想确保我不会失去

我的想法...


BTW - 我写的例子只是为了显示一个非常小的快速的例子

我的意思。我不认为有人会像我展示的那样做。


-Joe


" Joanna Carter [TeamB]" <乔**** @ not.for.spam>在消息中写道

news:uC ************* @ TK2MSFTNGP12.phx.gbl ...

" Joe" < JB ******* @ noemail.noemail> écritdansle message de news:
Oz ******* *******@TK2MSFTNGP12.phx.gbl ...

|我可能会过度劳累所以请原谅这个愚蠢的问题...
|
|说我做以下事情:
|
| DataTable table = new DataTable();
|
| myDataAdaptor.Fill(table);
|
| dataGrid1.DataSource = table;
|
| table = null;
|
|为什么dataGrid1.DataSource仍然指向一个表?
|
|如何将对象设置为null并具有对该对象的所有引用
现在
|是空的吗?

你不能。您只能将对象的引用设置为null。

如果您知道C,那么您会记住对象引用保存对象的
地址,您必须取消引用该指针才能获得
在C#中,所有对象都是垃圾收集的,你不能删除。他们。当
对给定对象的最后引用超出范围时,该对象将负责收集。你可以找到尽可能多的引用,但是只要* any *引用仍然包含
对象,对象就会保持活着。

在你的例子中, DataGris的DataSource正在保存对
表的引用,所以即使你将原始引用'table''置零,'table'指向的对象
仍保留由DataSource属性生存。

为什么要将刚刚分配的属性null为



Joanna

-
Joanna Carter [TeamB]
顾问软件工程师



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;

table = null;

Why does dataGrid1.DataSource still point to a table?

How can I set an object to null and have all references to that object now
be null?

Thanks,
Joe

解决方案

Joe,

dataGrid1.DataSource = table;

table = null;

Why does dataGrid1.DataSource still point to a table?
Because DataSource has its own copy of the reference to the table.
How can I set an object to null and have all references to that object now
be null?



You can''t. I frankly non of the languages that I know can do that unless the
classes are specially designed to use some kind of indirect reference where
they reference an object that holds the real reference. In this case
modifying the reference in this "proxy" class will cut off all the
references to the real object.
--

Stoitcho Goutsev (100)


"Joe" <jb*******@noemail.noemail> a écrit dans le message de news:
Oz**************@TK2MSFTNGP12.phx.gbl...

|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;
|
| table = null;
|
| Why does dataGrid1.DataSource still point to a table?
|
| How can I set an object to null and have all references to that object now
| be null?

You can''t. You can only set references to objects to null.

If you know C tthen you would remember that object references hold the
addresses of objects and you would have to dereference that pointer to get
at the object. You could "delete" the object and then the pointer would be
invalid and would have to be nulled to avoid AVs.

In C#, all objects are garbage collected, you can''t "delete" them. When the
last reference to a given object falls out of scope, the object is liable to
collection. You can certainly null as many references as you can find, but
the object will remain alive as long as *any* reference still holds that
object.

In your example, the DataGris''s DataSource is holding a reference to the
table, so even though you null the original reference, ''table'', the object
that ''table'' was pointing to is kept alive by the DataSource property.

Why would you want to null a property to which you have only just assigned a
valid object ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Hi,

Your answer is what I thought. I just wanted to make sure I wasn''t loosing
my mind...

BTW - The example I wrote was just to show a really small and quick example
of what I meant. I don''t think anyone would ever do as my example displayed.

-Joe

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:uC*************@TK2MSFTNGP12.phx.gbl...

"Joe" <jb*******@noemail.noemail> a écrit dans le message de news:
Oz**************@TK2MSFTNGP12.phx.gbl...

|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;
|
| table = null;
|
| Why does dataGrid1.DataSource still point to a table?
|
| How can I set an object to null and have all references to that object
now
| be null?

You can''t. You can only set references to objects to null.

If you know C tthen you would remember that object references hold the
addresses of objects and you would have to dereference that pointer to get
at the object. You could "delete" the object and then the pointer would be
invalid and would have to be nulled to avoid AVs.

In C#, all objects are garbage collected, you can''t "delete" them. When
the
last reference to a given object falls out of scope, the object is liable
to
collection. You can certainly null as many references as you can find, but
the object will remain alive as long as *any* reference still holds that
object.

In your example, the DataGris''s DataSource is holding a reference to the
table, so even though you null the original reference, ''table'', the object
that ''table'' was pointing to is kept alive by the DataSource property.

Why would you want to null a property to which you have only just assigned
a
valid object ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer



这篇关于将引用的对象设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆