C#对象传递的是引用,属性值已经改变

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConTest
{
    class ObjectOperation
    {
        public static void ChangeProperty(Student s)
        {
            s.Name = "I am";
            s.Age = 23;
        }
        public static void Main()
        {
            Student s = new Student(23, "甘全福");
            ChangeProperty(s);
            s.DisplayInfo();//输出Name:I amAge:23注意值已经改变
            Console.Read();
 
        }
 
    }
 
    class Student
    {
        public Student()
        {
            this.Age = 0;
            this.Name = string.Empty;
        }
 
        public Student(int age,string name)
        {
            this.Age = age;
            this.Name = name;
        }
        public int Age { getset; }
        public string Name { getset; }
        public void DisplayInfo()
        {
            Console.WriteLine("Name:" + this.Name + "Age:" + this.Age);
        }
 
    }
 
}
posted @ 2013-04-11 07:47  Predator  阅读(300)  评论(0编辑  收藏  举报