使用++运算符C#对字段数据进行有趣响应

时间:2012-10-05 13:01:28

标签: c# properties

我在这里有一个程序片段,允许创建一个简单的age,id,name和pay属性的Employee对象。只是玩弄它我注意到了

 Console.WriteLine(joe.Age+1); is my Main() method returns one, 

但是Console.WriteLine(joe.Age++);返回0.我知道每个构造函数的Age属性将被初始化为0,但是为什么不使用++运算符添加1? 编辑:我找到了奇怪行为的来源。在Age属性中,我应该empAge=Age它应该等于value

源:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmployeeApp
{
    class Employee
    {
        //field data
        //notice the the fields are declared as private
        //these fields are used in the constructors
        private string empName;
        private int empID;
        private float currPay;
        private int empAge;

        //properties! private field data should be accessed via public properties
        //note that properties don't use parentheses ()
        //within the set scope you see the 'value' contextual keyword
        //it represents the value being assigned by the caller and it will always be the same 
        //underlying data type as the property itself
        public int Age
        {
            get { return empAge; }
            set { empAge = Age; }
        }


        public string Name
        {
            get { return empName; }
            set
            {
                if (value.Length > 15)
                    Console.WriteLine("this name is too long.");
                else
                    empName = value;
            }
        }
        public int ID
        {
            get { return empID; }
            set { empID = value; }
        }
        public float pay
        {
            get { return currPay; }
            set { currPay = value; }
        }

        //constructors
        public Employee() { }

        public Employee(string name, int id, float pay, int age)
        {
            empName = name;
            empID = id;
            currPay = pay;
            empAge = age;
        }

        //methods
        //the int parameter that this method takes will come from somewhere in the Main method
        //currpay is a private field
        public void GiveBonus(float amount)
        {
            currPay += amount;
        }
        public void DisplayStats()
        {
            Console.WriteLine("name: {0}", empName);
            Console.WriteLine("ID: {0}", empID);
            Console.WriteLine("pay: {0}", currPay);
            Console.WriteLine("age: {0}", empAge);
        }
    }

}

这里的主要方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Encapsulation using traditional accessors/mutators or get/set methods
//the role of a get method is to return to the caller the current value of the underlying state data
//a set method allows the caller ot change the current value of the state data

//you need to have a getter and a setter for every field that the class has

namespace EmployeeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("fun with encapsulation");
            //Employee emp = new Employee("marvin", 456, 4000, 56);
            //emp.GiveBonus(3);
           // emp.DisplayStats();
           // emp.Name = "wilson";
           // emp.DisplayStats();

            Employee joe = new Employee();

            Console.WriteLine(joe.Age++);
        }
    }
}

4 个答案:

答案 0 :(得分:4)

++增量运算符有两个用途:

joe.Age++

++joe.Age

第一个,正如您正在使用的那样,在当前操作之后执行。因此,当您致电Console.WriteLine(joe.Age++);时,也可以使用以下代码表示:

Console.WriteLine(joe.Age);
joe.Age = joe.Age + 1;

所以,你将当前值传递给WriteLine,然后然后递增它。

++的前导将执行相反的操作 - 增量,然后使用该值。因此,Console.WriteLine(++joe.Age);也可以理解为:

joe.Age = joe.Age + 1;
Console.WriteLine(joe.Age);

答案 1 :(得分:3)

在变量之后使用unary ++ operator时,直到评估外部表达式之后才会添加。在变量之前使用它时,会在计算外部表达式之前进行添加。

例如,

// this will increment joe.Age, and then write it to console.
Console.WriteLine(++joe.Age);

// this will write joe.Age to the console, and then increment it.
Console.WriteLine(joe.Age++);

来自docs on msdn

  

第一种形式是前缀增量操作。结果   operation是操作数增加后的值。

     

第二种形式是后缀增量操作。结果   operation是操作数增加之前的值。

答案 2 :(得分:1)

在C ++和C#中,有两个++运算符。第一个是前缀运算符(++ age),这个按预期工作 - 递增值然后返回结果。后缀运算符(age ++)递增值但返回之前的值。

答案 3 :(得分:1)

在您的Age属性中,您不会将empAge成员更改为传入的值。这可能是您多次尝试++时未看到任何更改的原因。

public int Age 
{ 
    get { return empAge; } 
    set { empAge = Age;  } // this does not set the value!
} 

改为使用value

public int Age 
{ 
    get { return empAge;   } 
    set { empAge = value;  } // use the value passed in
} 

正如其他人所指出的那样,您正在使用++运算符的后缀版本。在将属性写入控制台之前,前缀版本将首先增加金额。