Datarow对象,为什么这段代码有效? [英] Datarow object, why is this code working?

查看:62
本文介绍了Datarow对象,为什么这段代码有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将datatabe的行值分配给新的数据行对象后,此对象的任何更改都会影响数据表,为什么?



查看这段代码。



after assign a row values of datatabe to a new data row object , any changes in this object affects on the data table , Why ?

have a look at this code.

DataRow dr = mydataset.Tables["NewTable"].Rows[0];
dr["Name"] = TextBox2.Text;





现在,如果您尝试调试应用程序,您会发现原始数据表[NewTable]具有新值hello而不是旧值虽然我们没有将上面的数据行返回到数据表(我们没有保存)



我只是需要澄清,还需要知道&了解当我创建数据行的Object时实际发生了什么。



Now if you try to debug your application you will find that the original data table ["NewTable"] has the new value "hello" instead of the old value Although we didn't return back the above data row to data table (we didn't save it )

I Just need a clarification and also need to know & understand what actually happen when i create an Object of a data row .

推荐答案

好的,这将变得有趣,因为它是.NET工作原理的基础。



DataRow是一个类 - 这意味着它被称为引用类型而不是值类型(例如)整数。

创建整数变量并为其赋值:

OK, this is going to get interesting, because it's kinda fundamental to how .NET works.

DataRow is a Class - which means it's what's called a "Reference type" rather than a "Value type" as (for example) integers are.
When you create an integer variable and assign it a value:
int x = 5;
int y = x;

您创建一个新变量并将值复制到其中:所以如果您这样做:

You crate a new variable and copy the value into it: so if you then do this:

x = x + 10;
Console.WriteLine("{0}:{1}", x, y);

它将打印:

It will print:

15:5

因为 y 获得了 x 内容的副本,而不是对它的引用。这是你所期望的!



参考类型不同:

Because y got a copy of the content of x, not a reference to it. This is what you expect!

Reference types are different:

public class MyClass
   {
   public int Val;
   }
...
MyClass x = new MyClass();
x.Val = 5;
MyClass y = x;

x.Val = x.Val + 10;
Console.WriteLine("{0}:{1}", x.Val, y.Val)

这次打印

This time it prints

15,15

因为 y 获得了 x 内容的副本,它是对实际实例的引用。因此,当您通过x变量更改Val时,它还更改了y引用的实例的Val,因为它们都引用了相同的实例!



相同事情发生在你的代码中:

Because y got a copy of the content of x, which was a reference to the actual instance. So when you changed the Val via the x variable, it also changed the Val of the instance that y referred to because they are both referencing the same instance!

The same thing is happening in your code:

DataRow dr = mydataset.Tables["NewTable"].Rows[0];

创建一个名为的新变量dr ,并将对Table行的引用的副本分配给它 - 它们都引用相同的内存区域,一行的相同实例。

那么那么:

Createa a new variable called dr, and assigns a copy of the reference to the Table row to it - they both refer to the same area of memory, the same instance of a Row.
So then:

dr["Name"] = TextBox2.Text;

更改唯一的实例 - 这会影响表中的实例,因为它是同一个实例!



有意义吗?





这里有一个更详细的区别描述:使用结构和类 - 这是怎么回事? [ ^ ]

[/ edit]

Changes the one and only instance - which affects the instance in the table because it's the same one!

Does that make sense?

[edit]
There is a more detailed description of the difference here: Using struct and class - what's that all about?[^]
[/edit]


这篇关于Datarow对象,为什么这段代码有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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