C# exercises and solutions- delete record

C# structure: student records application

Delete record

Step6: Defining the delete(student[] st, ref int itemcount) method to delete a target record from the array of student objects. The user will be prompted to enter the id of student record that his/her want to delete. Then this id will be checked to make sure it does exist in the list. If the target record or element really exists, the deletion process can be made. The deletion process starts by checking whether the target record is the last record, beginning or middle record. If the target record is the last record in the list, we simply delete the record by supplying it to the clean(student[] st, int index) method. The last record is the record that has it index equal to itemcount subtracted by 1. If the target record stays at the beginning or in the middle of the list, we need to use a loop to allow the previous element to take over the next element. This process continue until it reaches the end of the list(itemcount-1). Then the clean() method is called to clean the last element of the list that should not exit. After the element is cleaned, the itemcount variable decreases by 1. This means that the number of elements in the list decreases.

static void delete(student[] st, ref int itemcount)
string id;
int index;
if (itemcount > 0)
Console.Write("Enter student's ID:");
id = Console.ReadLine();
index = search(st, id.ToString(),itemcount);

if ((index!=-1) && (itemcount != 0))
if (index == (itemcount-1)) //delete the last record

clean(st, index);

Console.WriteLine("The record was deleted.");
else //delete the first or middle record
for (int i = index; i < itemcount-1; i++)
st[i] = st[i + 1];
clean(st, itemcount);
--itemcount ;


else Console.WriteLine("The record doesn't exist. Check the ID and try again.");

else Console.WriteLine("No record to delete");

static void clean(student[] st,int index)
st[index].stnumber = null;
st[index].stname = null;
st[index].sex = null;
st[index].quizz1 = 0;
st[index].quizz2 = 0;
st[index].assigment = 0;
st[index].midterm = 0;
st[index].final = 0;
st[index].total = 0;



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.