C#中的方法参数

huangapple go评论62阅读模式
英文:

Method parameter in C#

问题

在调用方法的代码中,调用方法中的类实例具有一个地址,调用方法的参数中包含该地址的副本,而且两个地址都指向同一个对象。

英文:

I was reading the official C# documentation by Microsoft on the topic of Method Parameters and I came across this paragram.

> A class instance is a reference type, not a value type. When a reference type is passed by value to a method, the method receives a copy of the reference to the class instance. That is, the called method receives a copy of the address of the instance, and the calling method retains the original address of the instance. The class instance in the calling method has an address, the parameter in the called method has a copy of the address, and both addresses refer to the same object. Because the parameter contains only a copy of the address, the called method cannot change the address of the class instance in the calling method. However, the called method can use the copy of the address to access the class members that both the original address and the copy of the address reference. If the called method changes a class member, the original class instance in the calling method also changes.

The above-highlighted line confuses me.

My question is?

  • What is a called method and calling method in C#?

In line The class instance in the calling method has an address, the parameter in the called method has a copy of the address, and both addresses refer to the same object.

Where is the class instance in the calling parameter located in the syntax?

Answer with some example will be highly appreciated!!

答案1

得分: 1

也许一个例子会更清晰:

Cake CreateCake(int orderNo){
   var order = new Cake(orderNo);
   Decorate(order);
   return order;
}
void Decorate(Cake order){
   ...
}

在这种情况下,DecorateCreateCake 调用,所以前者是被调用的方法,后者是调用的方法。

需要注意的重要一点是,两个 order 变量都将指向同一个蛋糕。还要注意的是,在程序实际运行之前,不会有任何蛋糕存在。源代码就像是将会发生的事情的配方。'类'指的是蛋糕的类型,或者是创建特定类型蛋糕的配方。'实例'或'对象'指的是你可以吃的实际蛋糕。

然而,搬动真实的蛋糕在面包店里有点麻烦,所以我们通常把所有的蛋糕放在一个存储位置,只传递一张写有存储位置的纸条。order 变量就是指向这张纸条。你可以轻松地复制订单并将副本交给装饰部门,但两份副本都会指向同一个存储位置。

英文:

Maybe an example makes it clearer:


Cake CreateCake(int orderNo){
   var order = new Cake(orderNo);
   Decorate(order );
   return order ;
}
void Decorate(Cake order ){
   ...
}

Decorate in this case is called by CreateCake, so the former is the called method, and the later the calling method.

The important thing to note is that both of the order variables will point to the same Cake. Also note that no cakes will exist until the program is actually run. The source code are just like a recipe for what will happen. 'Class' refers to the type of cake, or to the recipe to create a specific type of cake. 'Instance' or 'object' means an actual physical cake that you can eat.

However, moving actual cakes around the bakery is kind of cumbersome, so we usually put all the cakes in a storage location, and only pass around a piece of paper with the storage location noted down. The order variable refers to this piece of paper. You can easily copy the order and give the copy to the decorating department, but both copies will point to the same storage location.

答案2

得分: 0

公共静态无返回值 主(string[] 参数)
{
    ...
    // 调用 MyMethod 方法:
    MyMethod(一些参数);
    ...
}

// 这是上面调用的方法:
公共无返回值 MyMethod(某类型 一些参数)
{
   ...
   一些参数.某属性 = 新值;
   ...
}
英文:
public static void Main(string[] args)
{
    ...
    // Calling MyMethod:
    MyMethod(someParams);
    ...
}

// This is the called method above:
public void MyMethod(SomeType someParam)
{
   ...
   someParam.SomeProperty = newValue;
   ...
}

huangapple
  • 本文由 发表于 2023年3月3日 21:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定