且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将一个对象数组转换为另一个对象数组

更新时间:2022-11-25 11:38:59

您好,



以下是您的解决方案:



商务课程:

Hi,

Following is your solution:

Business Classes:
public class student
{
    public string name { get; set; }
    public string address { get; set; }
}

public class CollegeDetails
{
    public string name { get; set; }
    public student[] students { get; set;   }
}





实施:



Implementation:

// Array created for students i.e. Three studetns.
        student[] arrStudent = new student[3];

        // Here you also can create an array for College Details for example I created one single object.
        CollegeDetails objOneCollage = new CollegeDetails();

        // for loop to create three students quickly
        for (int i = 0; i < arrStudent.Length - 1; i++)
        {
            //for each student we have to create new object.
            student objStudent = new student();
            objStudent.address = "Address for Student " + i.ToString();
            objStudent.name = "Name for Student " + i.ToString();

            //Assign new student details to students array
            arrStudent[i] = objStudent;
        }

        // Now assign all students array to College object. if it is in array or single you can assign according.
        objOneCollage.students = arrStudent;

        //Now you can access all objects here
        for (int i = 0; i < objOneCollage.students.Length - 1; i++)
        {
            Response.Write(String.Format("Student Name:{0} \t Student Address:{1} </br>", objOneCollage.students[i].name, objOneCollage.students[i].address));
        }