Creating a Student Records using Struct in C#(Sharp)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Studentinfo
{
class Program
{
/*
* we are creating the structure of student.
* using the loop for taking the student record.
* using the Array.sort() for sorting the values.
*/
struct student
{
public string Names;
public DateTime Date;
public string Class;
}
static void Main(string[] args)
{
student[] sr = new student[3];
for (int i = 0; i < sr.Length; i++)
{
Console.Write("Enter the Name\t");
sr[i].Names = Console.ReadLine();
Console.Write("Enter the DOB\t");
sr[i].Date = Convert.ToDateTime(Console.ReadLine());
Console.Write("Enter the Class\t");
sr[i].Class = Console.ReadLine();
}
Console.WriteLine("Sort by Name Press 1 or DOB for 2");
char val = Convert.ToChar(Console.ReadLine());
switch (val)
{
case '1': Console.WriteLine("orderd by name"); Array.Sort(sr, (x, y) => String.Compare(x.Names, y.Names)); break;
case '2':Console.WriteLine("orderd by dob");Array.Sort(sr, (x1, y1) => DateTime.Compare(x1.Date, y1.Date));break;
default:Console.WriteLine("As it is");break;
}
for (int i = 0; i < sr.Length; i++)
{
Console.Write("\t Names\t{0}", sr[i].Names);
Console.Write("\t DOb\t{0}", sr[i].Date);
Console.Write("\t Class\t{0}", sr[i].Class);
Console.WriteLine("");
}
Console.ReadLine();
}
}
}

No comments:
Post a Comment