获取,设置和C#.NET值关键字关键字、NET

2023-09-03 04:03:05 作者:放荡不羁.

什么是这里的关键字,它是怎么分配的值​​ _num ?我是pretty的困惑,请给以下code中的说明。

 私人诠释_num;
    公众诠释NUM
    {
        得到
        {
            返回_num;
        }
        组
        {
            _num =价值;
        }
    }

    公共无效的button1_Click(对象发件人,EventArgs的)
    {
        NUM = numericupdown.Value;
    }
 

解决方案

在属性setter的背景下,关键字重新presents被分配的值的财产。它实际上是设置访问的隐含参数,就好像是这样的声明:

 私人诠释_num
公众诠释NUM
{
    得到
    {
        返回_num;
    }
    集(int值)
    {
        _num =价值;
    }
}
 
87个C 帮助类,各种功能性代码 转载自微信公众号 dotNET全栈开发

属性访问实际上是方法等同于:

 公众诠释get_num()
{
    返回_num;
}

公共无效set_num(int值)
{
    _num =价值;
}
 

What is value keyword here and how is it assigning the value to _num? I'm pretty confused, please give the description for the following code.

    private int _num;
    public int num
    { 
        get 
        {
            return _num;
        }
        set 
        {
            _num=value;
        }
    }

    public void button1_click(object sender,EventArgs e)
    {
        num = numericupdown.Value;
    }

解决方案

In the context of a property setter, the value keyword represents the value being assigned to the property. It's actually an implicit parameter of the set accessor, as if it was declared like this:

private int _num
public int num
{ 
    get 
    {
        return _num;
    }
    set(int value)
    {
        _num=value;
    }
}

Property accessors are actually methods equivalent to those:

public int get_num()
{
    return _num;
}

public void set_num(int value)
{
    _num = value;
}