C#中的成员方法 C#中的成员方法也称成员函数声明在类中与函数的声明方法类似用于描述对象的行为受到访问修饰符的限制。成员方法的具体声明如下using System; namespace Leson03 { class Person() { public int age; public string name; public Person[] friends; public void Speak(string a) { Console.WriteLine({0}说{1},name,a); } public bool IsAdult() { return age 18; } //较复杂的成员方法 public void AddFriends(Person p) { if (friends null) { friends new Person[] { p }; } else { Person[] newFriends new Person[friends.Length 1]; for (int i 0; i friends.Length; i) { newFriends[i] friends[i]; } newFriends[newFriends.Length - 1] p; friends newFriends; } } } class Program { static void Main(string[] args) { Console.WriteLine(成员方法); Person p1 new Person(); p1.name 张三; p1.age 18; p1.Speak(你好); p1.IsAdult(); Person p2 new Person(); p2.name 李四; p1.age 18; p1.AddFriends(p2); for (int i 0; i p1.friends.Length; i) { Console.WriteLine(p1.friends[i].name); } } } }需要注意的是成员方法必须在实例化对象以后由对象来调用相当于对象进行了某个行为。