using System;
using System.Collections;
using System.Collections.Generic;
class Student
{
private int studentid;
private string name;
private sbyte age; public int StudentID
{
get { return studentid; }
set { studentid = value; }
}
public string Name
{
get { return name; }
set{name=value;}
} public sbyte Age
{
get { return age; }
set { age = value; }
}
} class Class
{
public static IList<Student> members;
static Class()
{
Student stu = new Student();
stu.Name = "张三";
stu.Age = 18;
Student stu2 = new Student();
stu2.Name = "李四";
stu2.Age = 15;
members.Add(stu);
members.Add(stu2);
} public static bool InsertStudent(Student student)
{
try
{
members.Add(student);
return true;
}
catch
{
return false;
}
} public static Student GetStudent(string name)
{
return new Student();
//怎么写?
} public static Student GetStudent(int index)
{
return new Student();
//怎么写
}
}class App
{
static void Main()
{
Student stu = new Student();
stu.Name = "a";
stu.Age = 1;
foreach (Student stu2 in Class.members)//“Class”的类型初始值设定项引发异常。
{
Console.WriteLine(stu2);
}
}
}
using System.Collections;
using System.Collections.Generic;
class Student
{
private int studentid;
private string name;
private sbyte age; public int StudentID
{
get { return studentid; }
set { studentid = value; }
}
public string Name
{
get { return name; }
set{name=value;}
} public sbyte Age
{
get { return age; }
set { age = value; }
}
} class Class
{
public static IList<Student> members;
static Class()
{
Student stu = new Student();
stu.Name = "张三";
stu.Age = 18;
Student stu2 = new Student();
stu2.Name = "李四";
stu2.Age = 15;
members.Add(stu);
members.Add(stu2);
} public static bool InsertStudent(Student student)
{
try
{
members.Add(student);
return true;
}
catch
{
return false;
}
} public static Student GetStudent(string name)
{
return new Student();
//怎么写?
} public static Student GetStudent(int index)
{
return new Student();
//怎么写
}
}class App
{
static void Main()
{
Student stu = new Student();
stu.Name = "a";
stu.Age = 1;
foreach (Student stu2 in Class.members)//“Class”的类型初始值设定项引发异常。
{
Console.WriteLine(stu2);
}
}
}
static Class()
{
Student stu = new Student();
stu.Name = "张三";
stu.Age = 18;
Student stu2 = new Student();
stu2.Name = "李四";
stu2.Age = 15;
members.Add(stu);
members.Add(stu2);To:
public static List<Student> members;
static Class()
{
Student stu = new Student();
stu.Name = "张三";
stu.Age = 18;
Student stu2 = new Student(); stu2.Name = "李四";
stu2.Age = 15;
members = new List<Student>();
members.Add(stu);
members.Add(stu2);
}
{
foreach(Student student in members)
{
if(student.Name.Equals(name))
{
return student ;
}
} } public static Student GetStudent(int index)
{
members[index];
}
foreach (Student stu2 in Class.members)
//“Class”的类型初始值设定项引发异常。//Class.members没有初始化当然会有异常. 好的编程习惯是:每次声明集合的时候都进行初始化
//程序可以做以下修改:public static IList<Student> members = new Collction<Student>();
{
Console.WriteLine(stu2);
}
{
foreach(Student student in members)
{
if(student.Name.Equals(name))
{
return student ;
}
}
return null; } public static Student GetStudent(int index)
{
return members[index];
}
foreach (Student stu2 in Class.members)
//“Class”的类型初始值设定项引发异常。 //Class.members没有初始化当然会有异常. 好的编程习惯是:每次声明集合的时候都进行初始化
//程序可以做以下修改:public static IList <Student> members = new Collction <Student>();
{
Console.WriteLine(stu2);
}
using System;
using System.Collections;
using System.Collections.Generic;
class Test
{
static void Main()
{ }
}static class App
{
private static IList<Student> students = new List<Student>();
static App()
{
students.Add(new Student());
students.Add(new Student(2, "李四"));
}
public static IList<Student> ShowAllStudent()
{
return students;
} public static Student FindStudent(int PKID)
{
//怎么写
} public static Student FindStudent(string strName)
{
//怎么写
} public static Student UpdateStudnet(int PKID)
{
//怎么写
} public static Student DelStudent(int PKID)
{
//怎么写
}
}class Student
{
private int pkid;
private string name; public int PKID
{
get { return pkid; }
set { pkid = value; }
} public string Name
{
get { return name; }
set { name = value; }
} public Student()
{
pkid = 1;
name = "张三";
} public Student(int pkid, string name)
{
this.pkid = pkid;
this.name = name;
}
}
public static Student FindStudent(int PKID)
{
foreach(Student student in members)
{
if(student.PKID ==PKID)
{
return student ;
}
}
return null;
} public static Student FindStudent(string strName)
{
foreach(Student student in members)
{
if(student.Name.Equals(strName))
{
return student ;
}
}
return null;
} public static Student UpdateStudnet(int PKID)
{
//要修改成什么值? 方法参数有问题.
} public static Student DelStudent(int PKID)
{
Student currentStudent = FindStudent(PKID);
if(currentStudent !=null)
{
members.Remove(currentStudent);
}
return currentStudent;
}