为什么我不能在 setter 中输入属性名称而不是“值"?关键词? [英] Why I can't put a property name in setter instead of "value" keyword?

查看:50
本文介绍了为什么我不能在 setter 中输入属性名称而不是“值"?关键词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过 value 关键字在 setter 中分配一个值时,它起作用了.但是为什么当我尝试通过属性的名称分配值时它不起作用,它应该保持相同的分配值?

When I assign a value in the setter through the value keyword, it works. But why it doesn't work when I try to assign the value through the name of the property, which should hold the same assigned value?

在下面的例子中,我在 setter 中使用了 value 关键字,它工作得很好,我得到了预期的输出 -

In the following example, I use value keyword in setter, and it works just fine and I get the expected output -

private int i = 1;
public int prop
{ 
    get { return i; }
    set { i = value }
}

public void Print()
{
    Console.WriteLine(i)
}

static void Main ()
{
    Program p = new Program();
    p.prop = 5;
    Console.WriteLine(p.prop);   // outputs 5
    p.Print();                   // outputs 5
}

但是在下面的示例中,我使用属性名称 prop 而不是 value 关键字,并且它不会将新值分配给 i变量-

But in the following example, I use property name prop instead of value keyword, and it doesn't assign the new value to the i variable -

private int i = 1;
public int prop
{ 
    get { return i; }
    set { i = prop }
}

public void Print()
{
    Console.WriteLine(i)
}

static void Main ()
{
    Program p = new Program();
    p.prop = 5;
    Console.WriteLine(p.prop);   // outputs 1
    p.Print();                   // outputs 1
}

那么,有什么区别呢?为什么我不能使用应该包含新值的属性名称而不是 value 关键字?

So, what is the difference? Why I can't use name of the property, which should hold new value, instead of value keyword?

推荐答案

以下关于你的财产的假设 -

The following assumption about your property -

哪个应该保持相同的赋值

which should hold the same assigned value

基本上是错误的.您的属性本身根本没有任何值,它只是返回变量 i 的值.

is basically wrong. Your property itself is not holding any value at all, it just returns the value of the variable i.

要获得清晰的图片,请记住,C# 属性只是一对方法的语法糖,并且对于您的属性 -

To get a clear picture, keep in mind that C# properties are just syntactic sugar over a pair of methods, and for your property -

public int prop
{ 
    get { return i; }
    set { i = value }
}

幕后会生成两个方法,看起来像 -

two methods will be generated behind the scene, which look like -

public int get_prop()
{
    return this.i;
}

public void set_prop(int value)
{
    this.i = value;
}

如您所见,属性中的 value 关键字表示当您尝试为属性设置值时从外部传递的参数的通用名称.所以,当你做类似 -

As you can see, the value keyword in your property represents a generalized name for the parameter that is passed from outside when you try to set value to a property. So, when you do something like -

p.prop = 5;

基本上,set_prop 方法被调用时使用 5 作为参数 value 的值.

basically the set_prop method gets called with 5 as the value for the parameter value.

现在,当您尝试使用您的财产时 -

Now, when you are trying to use your property like -

public int prop
{ 
    get { return i; }
    set { i = prop }
}

生成的方法看起来像 -

the generated methods will look like -

public int get_prop()
{
    return this.i;
}

public void set_prop(int value)
{
    this.i = this.prop;
}

并且如您所见,这段代码完全忽略了从外部传递的 value 参数的值.

and as you can see, this code is totally ignoring the value of the value parameter passed from outside.

您的代码仍在设置该值.它正在调用 get 方法(返回 i 的值)并将 i 已经设置的值再次设置为 i.它只是没有使用传递给它的值.

Your code is still setting the value though. It is calling the get method (which returns the value of i) and setting the already set value of i to i again. It is just not making any use of the value that has been passed to it.

这篇关于为什么我不能在 setter 中输入属性名称而不是“值"?关键词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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