我被要求创建一些结构:学生,老师,课程,程序 然后创建一个数组来容纳5个学生结构,并为数组中学生的字段赋值,我坚持创建数组来保存结构,这里是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Module4Assignment
{
class Program
{
//Student structure:
public struct Student
{
public Student (string name , string address , string country , string birthday , int telephone)
{
this.Name = name;
this.Address = address;
this.Country = country;
this.Birthday = birthday;
this.Telephone =telephone;
}
public string Name;
public string Address;
public string Country;
public string Birthday;
public int Telephone;
}
//Teacher structure:
public struct Teacher
{
public Teacher(string tname, string taddress, string tcountry, string tbirthday, int ttelephone)
{
this.TName = tname;
this.TAddress = taddress;
this.TCountry = tcountry;
this.TBirthday = tbirthday;
this.TTelephone = ttelephone;
}
public string TName;
public string TAddress;
public string TCountry;
public string TBirthday;
public int TTelephone;
}
//Program structure
public struct Program
{
public Program(string pname , string department , int pcredits)
{
this.PName = pname;
this.Department = department;
this.PCredits = pcredits;
}
public string PName;
public string Department;
public int PCredits;
}
//Course structure
public struct Course
{
public Course(string cname, string day, int ccredits)
{
this.CName = cname;
this.Day = day;
this.CCredits = ccredits;
}
public string CName;
public string Day;
public int CCredits;
}
static void Main(string[] args)
{
//Instantiating 5 students structures:
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
Student student4 = new Student();
Student student5 = new Student();
//creating the array:
string[] studentArray = new string[5];
studentArray[0]=student1;
studentArray[1]=student2;
studentArray[2]=student3;
studentArray[3]=student4;
studentArray[4]=student5;
}
}
}
答案 0 :(得分:5)
我在这里做的事情有很多问题,但简单的答案是你不能把Student对象放到一个字符串数组中:
static void Main(string[] args)
{
//Instantiating 5 students structures :
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
Student student4 = new Student();
Student student5 = new Student();
//creating the array :
Student [] studentArray = new Student[5]; // <---- array of Student!
studentArray[0]=student1;
studentArray[1]=student2;
studentArray[2]=student3;
studentArray[3]=student4;
studentArray[4]=student5;
}
答案 1 :(得分:4)
static void Main(string[] args)
{
Student[] studentArray = new Student[5];
}
那就是它。您不需要显式创建和分配元素,因为Student
是一个结构(值类型)。
答案 2 :(得分:1)
你在上面的代码中所做的是重复只是创建一个结构数组,因为你应该创建5个具有原始结构属性的结构,所以你应该这样做
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EDXonline_AssignmentFour
{
class Program
{
static void Main(string[] args)
{
student[] studentArray = new student[5];
studentArray[0].FirstName = "einstein";
studentArray[0].LastName = "makuyana";
DateTime date1 = new DateTime(1993, 11, 22, 02, 00, 0);
studentArray[0].Birthdate = date1;
Console.WriteLine("student First Name: {0}", studentArray[0].FirstName);
Console.WriteLine("student Last Name: {0}", studentArray[0].LastName);
Console.WriteLine("student birthday: {0}", studentArray[0].Birthdate.ToString());
Console.ReadKey();
}
public struct student
{
// This is the custom constructor.
public student(string firstname, string lastname, DateTime birthdate)
{
this.FirstName = firstname;
this.LastName = lastname;
this.Birthdate = birthdate;
}
// These statements declare the struct fields and set the default values.
public string FirstName;
public string LastName;
public DateTime Birthdate;
}