一些更新从方法中被丢弃 [英] Some updates are discarded out of a method

查看:62
本文介绍了一些更新从方法中被丢弃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组从一个 BaseObject 继承的对象,这些对象具有一些全局原始属性(索引等).现在,我有两个对象,其中一个(目标一个)仅具有基本属性的值,另一个(源,它是一组对象中的一个)具有所有其他属性的值(从第三个派对应用),但基础的.

I have a set of objects inherits from one BaseObject which have some global primitive properties (indexes etc). Now, I have two objects, one of them (the target one) have values only for the base properties and the other (the source, which is one of a set of objects) have values for all other the properties (retrieved from a 3rd party app), but the base one's.

我试图将所有源对象的属性复制到目标对象,但保留目标的基本属性值.换句话说——我试图在不删除任何东西的情况下使两个对象的属性值相等……目标 = 源;只会删除目标的基本索引......因此,我创建了一个方法,它将两个对象(转换为 BaseObject)作为参数并将目标的索引值复制到复制前的源,如下所示:

I'm trying to copy all the source object's properties to the target one, but keep the target's base properties' values. In other words – I'm trying to equal the two object's properties' values without deleting anything… target = source; will just delete the target's base indexes… So, I made a method which gets as arguments the two objects (casted to BaseObject) and copy the target's indexes's values to the source befor the copy, like this:

Public void Copy(BaseObject source, BaseObject target)
{
    //Copy all primitive indexes here...
    source.index = target.index;
    source.reference = target.reference;
    etc…

    //Copy the updated object to the target one...    
    target = source;
}

在方法内部的调试模式下似乎没问题,但是 – 当我的代码退出方法时,我惊讶地发现虽然源对象已正确更新为继承和非继承属性,但目标对象值保持不变.因此,我不得不在方法之外再次将(更新的)源复制到目标中,如下所示:

It seems to be ok in debug mode inside the method, but – when my code exits from the method I was surprised to see that although the source object was correctly updated with both, inherited and non-inherited properties, the target one values left unchanged. So, I had to copy the (updated) source into the target again, outside the method, like this:

InheritedObject sourceObj = CreateObjectWithPrimitiveIndexes();
InheritedObject targetObj = GetObjectWithNoIndexesFrom3rdParty();

targetObj.Copy(source: sourceObj, target: targetObj);

//The targetObject is not updated, so I have to re-copy it outside the Copy() method
targetObj = sourceObj;

有人能解释一下为什么 sourceObj 被更新了,因为它是通过 ref 发送到 copy() 方法的,但是目标 obj 的行为就像它是由 val 发送的,并且它的所有更新都在方法之外被忽略了......??

Can someone explain me why the sourceObj is updated, as it is sent to the copy() method by ref, but the target obj behave like it is sent by val and all of its updates are ignored outside the method…???

我可以在方法签名等中使用 'ref'、'out' 关键字吗??

Sould I use 'ref', 'out' key words i nthe method signature etc??

推荐答案

如果你赋值给一个方法的参数,为了使调用者可以看到该赋值,该参数必须具有 ref(或 out)修饰符.请参阅 ref(C# 参考).

If you assign to a parameter of a method, for that assignment to be visible to the caller, the parameter must have the ref (or out) modifier. See ref (C# Reference).

示例:

// doesn't do anything!
void Copy(BaseObject target)
{
    ...
    target = Something;
}

// with ref, assignment is to the *same* variable as the caller gave
void Copy(ref BaseObject target)
{
    ...
    target = Something;
}

补充:

作为我提供的链接注释:

As the link I provided notes:

不要混淆按引用传递的概念引用类型的概念

Do not confuse the concept of passing by reference with the concept of reference types

这两个概念是正交的",如下表所示:

These two concepts are "orthogonal", as the following table illustrates:

                                            |                   |
                                            | ByVal (neither    |  ByRef (ref or
                                            | ref nor out):     |  out keyword):
                                            |                   |
--------------------------------------------+-------------------+----------------------
value type (struct, enum)                   | entire object     |  no copy, argument
                                            | is COPIED         |  must be a variable,
                                            |                   |  same variable used
--------------------------------------------+-------------------+----------------------
reference type (class, interface, delegate) | reference COPIED; |  no copy, argument
                                            | NEW reference to  |  must be a variable,
                                            | same object       |  same variable used
--------------------------------------------+-------------------+----------------------

如果你不使用可变结构,那么结构类型的变量只能通过重新赋值来改变,然后下面的经验法则对值类型和引用都有用类型:

If you don't use mutable structs, then variables of struct type can only ever change through re-assignemnt, and then the following rule-of-thumb is useful for both value types and reference types:

您需要ref(或out)关键字当且仅当您的方法分配(包括像 += 这样的复合赋值)到相关参数.

You need the ref (or out) keyword if and only if your method assigns (including compund assignment like +=) to the parameter in question.

另见C#中引用类型变量的ref"有什么用?.

这篇关于一些更新从方法中被丢弃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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