如何创建一个数组来保存结构?

How to create an array to hold structures?

我被要求创建一些结构:学生、教师、课程、程序 然后制作一个数组来保存 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;
        }
    }
}

我对你在这里做的事情有很多问题,但简单的答案是你不能将 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;
    }
static void Main(string[] args)
{
    Student[] studentArray = new Student[5];
}

就是这样。您不需要显式创建和分配元素,因为 Student 是一个结构(值类型)。

你在上面的代码中所做的只是重复创建一个结构数组,因为你应该创建 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;
        }