Lecture 11: Generics
The Generic List loading data from a file using System.IO; : namespace EmpListDemo { static class Program { static void Main(string[] args) { string fname = "empstable.txt"; string txtline; List Emps = new List (); // a generic list of Employee List Emps2 = new List (); // we will make a copy of Emps // reading employee data from a text file TextReader tr = new StreamReader(fname); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split(','); Emps.Add(new Employee(field[0].Trim(),field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4]))); } while (true); tr.Close(); :
// display the contents of the list Emps foreach (Employee emp in Emps) { Console.WriteLine("{0} {1} {2} {3} {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage); } Wade Boggs Robin Banks Jerry Mander Amanda Rekonwith Doug Wells Anita Break Juan Abrew Ben Dover Ilene Dover Displaying the Contents of the List Emps
// we are making a copy of Emps called Emps2 foreach (Employee emp in Emps) { Employee emp2 = new Employee(); emp2 = emp; Emps2.Add(emp2); } // so why not just assign one list to the other? // Emps2 = Emps; // // because this would not make a separate copy but // rather point both Emps2 and Emps to the same records! Making a Copy of a List
// we "tag" each record that passes our criteria foreach (Employee emp in Emps2) { if (emp.Age > 39 & emp.YrsEmp >= 10) emp.Tag = true; } // now we remove all records from Emps2 that HAVE NOT // been "tagged" i.e. remove those with emp.Tag = false // this construct is implemented using a delegate Emps2.RemoveAll(delegate(Employee emp) { return !emp.Tag; }); The RemoveAll Delegate Method age>39 and yrsemp >= 10 Wade Boggs Amanda Rekonwith Wade Boggs Robin Banks Jerry Mander Amanda Rekonwith Doug Wells Anita Break Juan Abrew Ben Dover Ilene Dover
// this is a LINQ query! var queryresults = from q in Emps where q.LastName.StartsWith("B") select q; // so what's in queryresults??? // let's take a look Console.WriteLine(); foreach (var q in queryresults) { Console.WriteLine(q.FirstName + " " + q.LastName); } Working with LINQ Wade Boggs Robin Banks Anita Break Wade Boggs Robin Banks Jerry Mander Amanda Rekonwith Doug Wells Anita Break Juan Abrew Ben Dover Ilene Dover
// let's try another example // notice we don't redefine the var queryresults queryresults = from q in Emps where q.Age>30 select q; // now what's in queryresults??? Console.WriteLine(); foreach (var q in queryresults) { Console.WriteLine(q.FirstName + " " + q.LastName); } Console.ReadKey(); Another LINQ Query Example Wade Boggs Robin Banks Amanda Rekonwith Doug Wells Juan Abrew Ben Dover Wade Boggs Robin Banks Jerry Mander Amanda Rekonwith Doug Wells Anita Break Juan Abrew Ben Dover Ilene Dover
// one more example queryresults = from q in Emps where ((q.Age>40 & q.Wage 25.0)) select q; Wade Boggs Doug Wells Wade Boggs Robin Banks Jerry Mander Amanda Rekonwith Doug Wells Anita Break Juan Abrew Ben Dover Ilene Dover
using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; namespace LoadArrayFromTextfile { class Program { static void Main(string[] args) { int[,] mat = new int[10,10]; string textline; int k; TextReader tr = new StreamReader("sample_01.txt"); for (int i = 0; i < 10; i++) { textline = tr.ReadLine(); k = 0; foreach (string str in textline.Split(' ')) { if (str != "") { mat[i, k] = Convert.ToInt32(str); k += 1; } tr.Close(); Loading an Array from a Text File for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write("{0} ", mat[i, j]); } Console.WriteLine(); } Console.ReadKey(); }
List Emps = new List (); string txtline; TextReader tr = new StreamReader("employees.txt"); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split('\t'); Emps.Add(new Employee(field[0].Trim(), field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4]))); } while (true); tr.Close(); Reading and Writing Textfiles
foreach (Employee emp in Emps) { Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName); } TextWriter tw = new StreamWriter("employees.txt"); foreach (Employee emp in Emps) { tw.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage); } tw.WriteLine("xxx"); tw.Close(); Reading and Writing Text Files continued
Dealing with Data cut & paste
Pasting into Word or PPT Preserves Cells
Pasting to a Text Editor Creates Separators e.g. Tabs