public void AddStudent(StudentStruct student) { StudentStruct[] s; int newLength; int i = 0; // if the students array has not been created yet, set the new length to 1, // otherwise newLength should be 1 + the current Length (to hold the new student we are adding) newLength = (this.students == null) ? 1 : (this.students.Length + 1); // create the array of the new length s = new StudentStruct[newLength]; // if the students array has existing values in it if (this.students != null) { // copy them into our new array foreach (StudentStruct st in this.students) { s[i] = st; ++i; } } // append our new student to the new array s[i] = student; // set our students array to point to the new array this.students = s; }
// Class Examples // Author: David Schuh // Purpose: Contains examples for Week 3 // Restrictions: Only contains code snippets. static void Main(string[] args) { // Method: Main // Purpose: The main entry point for the executable. Code snippet examples. // Restrictions: None /////////////////////////////////////////////////////// // Functions /////////////////////////////////////////////////////// { for (int i = 0; i < 10; ++i) { Console.WriteLine($"{i} is " + (isEven(i) ? "even" : "odd")); } Console.WriteLine(CalcAverage(true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); Console.WriteLine(CalcAverage(true, 1, 2)); int one = 0; int two; // function overloading and passing variables by value or by reference SetOne(one); SetOne(ref one); SetTwo(out two); // the TryParse() method! string sNumber = null; int nNumber; do { Console.Write("Enter a number: "); sNumber = Console.ReadLine(); } while (!int.TryParse(sNumber, out nNumber)); } /////////////////////////////////////////////////////// // Structs /////////////////////////////////////////////////////// { StudentStruct[] students = new StudentStruct[2]; students[0].eGender = GenderPronoun.her; students[0].eCollegeYear = CollegeYear.freshman; students[0].sName = "June Smith"; students[0].grade = 56.9; students[0].Password = "******"; //this will call the set of password property students[1].eGender = GenderPronoun.them; students[1].eCollegeYear = CollegeYear.junior; students[1].sName = "Jan Smith"; students[1].grade = 99.9; //this will use the English definition here in the foreach loop foreach (StudentStruct student in students) { Console.WriteLine($"{student.sName} | {student.eCollegeYear} | {student.eGender} | {student.grade}"); } } }