get,set and value keyword in c#.net

前端 未结 3 1928
夕颜
夕颜 2020-12-29 13:14

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.

3条回答
  •  滥情空心
    2020-12-29 13:58

    The value keyword is a contextual keyword, that is, it has a different meaning based on its context.

    Inside a set block, it simply means the value that the programmer has set it to. For instance,

    className.num = 5;
    

    In this case, value would be equal to 5 inside of the set block. So you could write:

    set
    {
        int temp = value; //temp = 5
        if (temp == 5) //true
        {
            //do stuff
        }
        _num = value;
    }
    

    Outside of a set block, you can use value as a variable identifier, as such:

    int value = 5;
    

    Note that you cannot do this inside a set block.

    Side note: You should capitalize the property num to Num; this is a common convention that makes it easier for someone who's reading your class to identify public and private properties.

提交回复
热议问题