简体   繁体   English

c# 传递 class 类型成员作为参数作为按值调用

[英]c# passing class type member as parameter works as call by value

as i know class type parameter is always passed as reference.据我所知,class 类型参数始终作为参考传递。 but whenever i pass a class type variable which is class member as parameter.但是每当我传递一个 class 类型变量时,它是 class 成员作为参数。 it always works like call by value.它总是像按值调用一样工作。

public class MapGenerator : MonoBehaviour
{
   [SerializeField]
    private List<MapEntity> stageTerrains = new List<MapEntity>();
    private MapEntity stageTerrain = new MapEntity();

    private void Start()
    {
        RandomChoice(stageTerrains);
    }


    public void RandomChoice(MapEntity mapEntity)
    { 
        mapEntity = stageTerrains[Random.Range(0,selectedList.Count)];
        Print(mapEntity);  //it always works well 
        Print(stageTerrain);  // always print null
    }
}

so i put an ref keyword to parameter and it works as call by reference所以我在参数中添加了一个 ref 关键字,它作为引用调用

public class MapGenerator : MonoBehaviour
{
   [SerializeField]
    private List<MapEntity> stageTerrains = new List<MapEntity>();
    private MapEntity stageTerrain = new MapEntity();

    private void Start()
    {
        RandomChoice(ref stageTerrains);
    }


    public void RandomChoice(ref MapEntity mapEntity)
    { 
        mapEntity = stageTerrains[Random.Range(0,selectedList.Count)];
        Print(mapEntity);  //it always works well 
        Print(stageTerrain);  // print well too 
    }
}

i wonder why passing class type member as parameter is worked like call by value我想知道为什么将 class 类型成员作为参数传递就像按值调用一样工作

From the ref documentation 从参考文档

When used in a method's parameter list, the ref keyword indicates that an argument is passed by reference, not by value.当在方法的参数列表中使用时,ref 关键字表示参数是通过引用而不是值传递的。 The ref keyword makes the formal parameter an alias for the argument, which must be a variable. ref 关键字使形参成为实参的别名,实参必须是变量。

So I'll try to explain the makes the formal parameter an alias as simple as possible.因此,我将尝试解释使形式参数尽可能简单的别名

Without ref没有参考

Imagine the stageTerrain from private MapEntity stageTerrain = new MapEntity();想象一下来自私有stageTerrain private MapEntity stageTerrain = new MapEntity();的 stageTerrain as a box holding an address.作为一个包含地址的盒子。

Imagine the mapEntity from public void RandomChoice(MapEntity mapEntity) as another NEW box.想象一下来自public void RandomChoice(MapEntity mapEntity) mapEntity mapEntity 作为另一个框。

  • If you change a property of the mapEntity before assigning a new object, it will also change the value of the calling variable.如果在分配新的 object 之前更改 mapEntity 的属性,它也会更改调用变量的值。

When you call the RandomChoice the mapEntity is a new box holding the same memory address as the stageTerrain box.当您调用RandomChoice时, mapEntity是一个新框,它与stageTerrain框具有相同的 memory 地址。

Now mapEntity = stageTerrains[Random.Range(0,selectedList.Count)];现在mapEntity = stageTerrains[Random.Range(0,selectedList.Count)]; will assign the selected value only to the New box.将仅将选定的值分配给“新建”框。

With ref带参考

Imagine the mapEntity from public void RandomChoice(ref MapEntity mapEntity) as an alias for an already existing box that will hold the memory reference just with a different name.想象一下来自public void RandomChoice(ref MapEntity mapEntity) mapEntity mapEntity 作为一个已经存在的盒子的别名,该盒子将使用不同的名称保存 memory 引用。 (thus the alias statement) (因此别名声明)

When you call the RandomChoice the mapEntity is the stageTerrain box.当您调用RandomChoice时, mapEntitystageTerrain框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM