public class Student
{
public string name;
public int ID;
}
public class telephoneList
{
public void fuzhi()
{
Student[] Students = new Student[3];
Students[0].name = "张三";
Students[0].ID = 10000;
Students[1].name = "李四";
Students[1].ID = 11000;
Students[2].name = "王五";
Students[2].ID = 12000;
}
public int this[string thename]
{
get
{
Student[] Students = new Student[3]; for (int i = 0; i < 4; i++)
{
if(Students[i].name ==(thename))
return Students[i].ID;
}
return 0;
}
}
}
public class output
{
static void Main()
{
telephoneList thetelephoneList = new telephoneList();
Console.WriteLine(thetelephoneList["张三"]);
}
}请问,我这个代码错在哪里,?
{
public string name;
public int ID;
}
public class telephoneList
{
public void fuzhi()
{
Student[] Students = new Student[3];
Students[0].name = "张三";
Students[0].ID = 10000;
Students[1].name = "李四";
Students[1].ID = 11000;
Students[2].name = "王五";
Students[2].ID = 12000;
}
public int this[string thename]
{
get
{
Student[] Students = new Student[3]; for (int i = 0; i < 4; i++)
{
if(Students[i].name ==(thename))
return Students[i].ID;
}
return 0;
}
}
}
public class output
{
static void Main()
{
telephoneList thetelephoneList = new telephoneList();
Console.WriteLine(thetelephoneList["张三"]);
}
}请问,我这个代码错在哪里,?
{
public string name{get;set;}
public int ID { get; set; }
}
public class telephoneList
{
Student[] Students = new Student[3];
public telephoneList()
{
for (int i = 0; i < 3; i++)
{
Students[i] = new Student();//实例化
}
Students[0].name = "张三";
Students[0].ID = 10000;
Students[1].name = "李四";
Students[1].ID = 11000;
Students[2].name = "王五";
Students[2].ID = 12000;
}
public int this[string thename]
{
get
{
for (int i = 0; i < 4; i++)
{
if (Students[i].name == (thename))
return Students[i].ID;
}
return 0;
}
}
} class Program
{
static void Main(string[] args)
{ telephoneList thetelephoneList = new telephoneList();
Console.WriteLine(thetelephoneList["张三"]); Console.ReadKey();
}
}
10000
{
for (int i = 0; i < 3; i++)
{
Students[i] = new Student();//实例化
}
请问,这一部分是不是不带参数的构造函数?不带参数的构造函数不是系统自己整的吗?如果是,在这里写个构造函数是啥意思呢,谢谢?
呢,不写其它样式呢?这不是构造函数吗
也可通过对象初始化器赋值
class IndexClass
{
private string[] name=new string[10];
public string this[int index]
{
get { return name[index]; }
set { name[index]=value; }
}
}
class test
{
static void Main()
{
IndexClass b=new IndexClass();
b[0]="张三";
b[1]="李四";
b[2]="王五";
Console.WriteLine("b[0]="+b[0]);
Console.WriteLine("b[1]="+b[1]);
Console.WriteLine("b[2]="+b[2]);
Console.ReadKey();
}
}这段代码,创建数组对象后,直接就赋值了,并没有一个一个new,为什么你帮我写的那个代码,要用个for循环来全部new一下呢?谢谢