参考文献与副本

编程入门 行业动态 更新时间:2024-10-27 14:20:35
本文介绍了参考文献与副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨大家好, 考虑以下代码行: 地址addr = ei.Address; 其中ei是一个具有地址 类型属性的类的实例。 问题是上述任务中发生了什么?与C / C ++的不同,似乎上面的代码指示编译器将addr变量指向 ei.Address已经 驻留,即,它以某种方式指向ei.Address。因此,对addi变量的任何 修改都会立即应用于ei.Address。 嗯,这是对的吗?或者,我错过了什么? 任何帮助都将受到高度赞赏, 干杯, Mehdi

Hi folks, Consider the following line of code: Address addr = ei.Address; where ei is an instance of a class that has got a property of Address type. The question is that what is happening in the above assignment? Unlike the C/C++, it seems that the above code instructs the compiler to point the addr variable to the same location where ei.Address already resides, i.e, it''s somehow a pointer to the ei.Address. Therefore, any modification on addr variable is instantly applied to the ei.Address. Well, is this right? or, am I missing something? Any help would be highly appreciated, Cheers, Mehdi

推荐答案

是的,这正是发生的事情 - 类有引用类型 语义,所以你的变量是(地址)的先决条件是:a(托管) 指向(托管)堆上对象的指针。 如果你想要值类型语义(即,在一个眼睛的眨眼间克隆的实例)然后结构虽然老实说一个地址感觉对我来说就像一个班级,但是很有用。也许看看ICloneable界面?即 公共类地址:ICloneable { 公共地址克隆(){//你的显式复制码到这里 } 对象ICloneable.Clone(){return this.Clone();} //非类型安全版 通常通过显式实现完成 } 然后你可以打电话: 地址addr = ei.Address.Clone(); Yes, that is exactly what is happening - classes have reference-type semantics, so your "variable" (address) is preceisly that: a (managed) pointer to an object on the (managed) heap. If you want value-type semantics (i.e. instances cloned at the blink of an eye) then "struct" can be useful, although to be honest an address "feels" like a class to me. Perhaps look at the ICloneable interface? i.e. public class Address : ICloneable { public Address Clone() { // your explicit copy code goes here } object ICloneable.Clone() {return this.Clone();} // non-typesafe version usually done via an explicit implementation } then you can call: Address addr = ei.Address.Clone();

" mehdi_mousavi" < me *********** @ gmailwrote in message news:11 ******************* ***@b28g2000cwb.googlegr oups ... Hi, "mehdi_mousavi" <me***********@gmailwrote in message news:11**********************@b28g2000cwb.googlegr oups... 问题是上述作业中发生了什么?与C / C ++的不同,似乎上面的代码指示编译器将addr变量指向 ei.Address已经 驻留,即,它以某种方式指向ei.Address。因此,对addi变量的任何 修改都会立即应用于ei.Address。 嗯,这是对的吗?或者,我错过了什么? The question is that what is happening in the above assignment? Unlike the C/C++, it seems that the above code instructs the compiler to point the addr variable to the same location where ei.Address already resides, i.e, it''s somehow a pointer to the ei.Address. Therefore, any modification on addr variable is instantly applied to the ei.Address. Well, is this right? or, am I missing something?

在托管世界中,它被称为引用,但是,这几乎是什么 发生如果地址是一个类;你在帖子中指出ei是一个类的一个 实例,你对地址一无所知。 如果Address不是一个类而是一个结构以上是不正确的。 - - Ignacio Machin, ignacio.machin AT dot。 state.fl.us 佛罗里达州交通局

In managed world it''s called reference but yes, that''s pretty much what happens IF Address IS A class ; in your post you especify that ei is an instance of a class, you say nothing about Address. If Address is not a class but a struct the above is not true. -- -- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation

有两种不同的方式来引用.Net中的数据:按价值,按 参考。这就是.Net平台定义引用类型和值 类型的原因。值类型(例如整数和结构)按值传递, 表示当 的实例传递类型中数据的副本时该类型被引用。构成大多数.Net 类型的引用类型是通过引用传递的。这意味着当引用该类型的实例时,将传递一个指向 实例的托管指针。 要传递引用类型的副本,实例必须复制或克隆。 - HTH, Kevin Spencer 微软MVP 鸡肉沙拉手术 你所寻求的是什么。 " mehdi_mousavi" < me *********** @ gmailwrote in message news:11 ******************* ***@b28g2000cwb.googlegr oups ... There are 2 distinct ways to reference data in .Net: By value, and by reference. This is why the .Net platform defines reference types and value types. Value types, such as integers and structures, are passed "by value," meaning that a copy of the data in the type is passed when an instance of that type is referenced. Reference types, which constitute most of the .Net types, are passed "by reference," meaning that a managed pointer to the instance is passed when an instance of that type is referenced. To pass a copy of a reference type, the instance must be copied, or Cloned. -- HTH, Kevin Spencer Microsoft MVP Chicken Salad Surgery What You Seek Is What You Get. "mehdi_mousavi" <me***********@gmailwrote in message news:11**********************@b28g2000cwb.googlegr oups... 嗨大家好, 考虑以下代码行: 地址addr = ei.Address; 其中ei是一个具有地址属性的类的实例 类型。 问题是上述作业中发生了什么?与C / C ++的不同,似乎上面的代码指示编译器将addr变量指向 ei.Address已经 驻留,即,它以某种方式指向ei.Address。因此,对addi变量的任何 修改都会立即应用于ei.Address。 嗯,这是对的吗?或者,我错过了什么? 任何帮助都将受到高度赞赏, 干杯, Mehdi Hi folks, Consider the following line of code: Address addr = ei.Address; where ei is an instance of a class that has got a property of Address type. The question is that what is happening in the above assignment? Unlike the C/C++, it seems that the above code instructs the compiler to point the addr variable to the same location where ei.Address already resides, i.e, it''s somehow a pointer to the ei.Address. Therefore, any modification on addr variable is instantly applied to the ei.Address. Well, is this right? or, am I missing something? Any help would be highly appreciated, Cheers, Mehdi

更多推荐

参考文献与副本

本文发布于:2023-10-09 10:39:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475512.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:副本   参考文献

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!