Lecture 11: Generics. The Generic List loading data from a file using System.IO; : namespace EmpListDemo { static class Program { static void Main(string[]

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.
תכנות ב C#. דוגמא לפלט using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void.
In C# program Before you can start using the ODBC class definitions, you will need to include the right module. using System.Data.Odbc; // ODBC definitions.
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
1 Binary Files ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Computer Programming 1.  Editor Console Application Notepad Notepad++ Edit plus etc.  Compiler & Interpreter Microsoft.NET Framework  Microsoft visual.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Based on material from Telerik Corporation.
Web Controls Making a WebRequest using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO;
Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
LINQ AND GENERIC COLLECTIONS DR. JOHN P. ABRAHAM PROFESSOR UTPA.
Generics Ashima Wadhwa. What are generics Generics were added by C# 2.0 the term generics means parameterized types. Using generics, you can define a.
Text Files and String Processing
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Building Your Own Class. We will build a simple instance class for an inventory control application and a main program to test it. We need an Item class.
CSC 298 Streams and files.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Strings in C++/CLI us/library/system.string.aspxhttp://msdn.microsoft.com/en- us/library/system.string.aspx public: static.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
File Output Writing data out to a file 1. Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination.
Staples are our staple Building upon our solution.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
התוכנית: using System; using System.Collections.Generic;
using System; namespace Demo01 { class Program
C# Arrays.
Building Your Own Class
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
MUTENESS ASSİSTMENT 1)WHY CHOICE ? 2)ABOUT DESİGN 3)WHICH CODES USING
Functions Used to write code only once Can use parameters.
Classes Variables That Are Not of a Built-in Type Are Objects
عرض اجمالي المهام الشرطية في سي شارب (الأمر if)
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections UTPA – Fall 2012 This set of slides is revised from lecture slides.
WaysInJavaToParseXML
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
class PrintOnetoTen { public static void main(String args[]) {
Structured Variables & File Systems
Module 2 Variables, Assignment, and Data Types
When an argument to method is an entire array or an individual element of reference type, the called method receives a copy of reference. However an argument.
Advanced .NET Programming I 6th Lecture
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

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