参考类型的变量..本体论问题

时间:2011-01-19 14:41:20

标签: c# terminology

拥有以下代码:

 class X
    {
      public void Y(){}
    }


    X _x=new X();
    _x.Y();  //should I say Y is method of _x variable? It is easy but actually the variable contains just reference to object that has this method

   X newX=x;   //here I assign the value of variable x to variable newX. The value is reference 

3 个答案:

答案 0 :(得分:1)

Y是在X引用的类X的实例上调用的类_x上的pubilc实例方法。

顺便说一句,像这样的调用实际上被编译为类似

的东西
call X::Y(_x)

这是因为每个实例级方法都有一个隐式的第一个参数,它是对正在调用该方法的对象的引用(这就是this引用正确对象的方式)。

答案 1 :(得分:1)

在我最迂腐的情绪中,我会写:

  

Y是在X类型中声明的无参数实例方法。它是在_x的值引用的对象上调用的。

_x本身既不是对象也不是引用 - 它是变量。)

答案 2 :(得分:1)

通常情况并不是很大,因为类和变量都有描述性名称,因此很清楚它是什么。

使用实际类的示例:

builder是一个变量,包含对StringBuilder类实例的引用:

StringBuilder builder = new StringBuilder();

Append方法是StringBuilder类中的方法,而不是builder变量中的方法。您正在builder所指的实例上调用该方法:

builder.Append("asdf");
相关问题