学习了基本语法,学了什么大概忘记了,做了什么也基本忘了。自学了集合吧
namespace un_lesson2_jihe
{
class MainClass
{
public static void Main (string[] args)
{
//集合-数组
//堆存放变量,栈保留变量的地址
ArrayList list = new ArrayList ();
Student stu = new Student (10,"wangyi",1.2);
Student stu1 = new Student (11, "xiaoer",2.5);
Student stu2 = new Student (12, "xiaosan", 2.0);
// list.Add (5);
// list.Add (8);
// list.Add (3);
// list.Add (0);
list.Add (stu);
list.Add (stu1);
// list.Add (stu2);
//list.Add ("a");
list.Sort ();
//list.Reverse ();
for (int i = 0; i < list.Count; i++) {
Console.WriteLine (list[i]);
}
}
}
//通过接口编写自建类的对比方法
public class Student:IComparable
{
public int CompareTo(object obj)
{
if (obj is Student) {
Student s = (Student)obj;
Console.WriteLine (this.Score);
return this.Score.CompareTo (s.Score);
} else {
return 1;
}
}
// public override string ToString ()
// {
// return "id:"+id+"\tname:"+name+"\tScore:"+Score;
// }
//重写自建类的ToString方法,得以直接输出到控制台
public override string ToString ()
{
return string.Format ("[Student: id={0}, name={1}, Score={2}]", id, name, Score);
}
public int id{ get; set;}
public string name{ get; set;}
public double Score{ get; set;}
public Student(int id,string name,double score)
{
this.id = id;
this.name = name;
this.Score = score;
}
public Student()
{
}
}
}