1. Trang chủ
  2. » Giáo Dục - Đào Tạo

C lab08

5 113 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

PROGRAMMING IN C# Module 14: Generics and Iterators Module 15: Anonymous methods, Partial and Nullable type Lab Guide for Lab8 Session Objectives In this session, you will be practicing with  Generics  Anonymous methods  Partial classes  Nullable type Part – Getting started (30 minutes) Using generic class Following an application has two class Product and Program Class Product has fields:  string name  double cost  int onhand Class Program in Main method creates a List of Product and adds some products to list and finally prints to screen Scan the code first, type the code, compile, run and observe the result Create class Car using System; class Product { private string name; private double cost; private int onhand; public Product(string n, double c, int h) { name = n; cost = c; onhand = h; } public override string ToString() { return String.Format("{0,-10}Cost: {1,6:C} On hand: {2}", ©2011- FPT Aptech Page 1/4 name, cost, onhand); } } Create class Program class Program { public static void Main() { List inv = new List(); // Add elements to the list inv.Add(new Product("A", 5.9, 3)); inv.Add(new Product("B", 8.2, 2)); inv.Add(new Product("C", 3.5, 4)); inv.Add(new Product("D", 1.8, 8)); Console.WriteLine("Product list:"); foreach (Product i in inv) { Console.WriteLine(" " + i); } } } Creating generic class This application create class Class Pair is a generic class that has two properties of one type Class Program creates a instance of Pair and uses its properties Scan the code first, type the code, compile, run and observe the result Class Pair class Pair { private T first; private T second; public Pair(T first, T second) { this.first = first; this.second = second; } public T First { ©2011- FPT Aptech Page 2/4 get { return first; } } public T Second { get { return second; } } } Class Program class Program { public static void Main(string[] args) { Pair pair = new Pair("An","Nga"); Console.WriteLine("({0},{1})", pair.First, pair.Second); Console.ReadLine(); } } Creating generic method This application creates a method that swaps values of two variable of two same type and tests it Scan the code first, type the code, compile, run and observe the result class Program { public static void Main(string[] args) { String a = "a"; String b = "b"; Swap(ref a, ref b); System.Console.WriteLine(a + " " + b); } static void Swap(ref T lhs, ref T rhs) { T temp = lhs; lhs = rhs; rhs = temp; } } Using nullable type The application creates method get first element of a list that uses nullable type for int and tests it Scan the code first, type the code, compile, run and observe the result class Program { public static void Main(string[] args) ©2011- FPT Aptech Page 3/4 { //Test in case return null Console.WriteLine("{0}",Min(null)); //Add data to list List list = new List(); list.Add(10); list.Add(1); list.Add(1230); //Test in case does not return null Console.WriteLine("{0}", GetFirst(list)); } private static int? GetFirst(List list) { if (list==null || list.Count==0) { return null; } return list[0]; } } Part – Workshops (30 minutes) • • Quickly look at workshowps of Module 14 and Module 15 Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part – Lab Assignment (60 minutes) Do the assignment for Module 14 and Module 15 carefully Discuss with your class-mates and your instructor if needed Part – Do it your self Exercise 1: Create a new source file In a method, declare a variable temperatures of type List Add some numbers to the list Write a foreach loop to count the number of temperatures that equal or exceed 25 degrees Write a method GreaterCount with signature: static int GreaterCount(List list, double min) { //TODO: } that returns the number of elements of list that are greater than or equal to Call the method on your temperatures list Exercise 2: Write a method with signature ©2011- FPT Aptech Page 4/4 static int GreaterCount(IEnumerable eble, double min) { //TODO: } that returns the number of elements of the enumerable that are greater than or equal to Then call the method on an array of type double[] ©2011- FPT Aptech Page 5/4

Ngày đăng: 27/10/2019, 09:57

Xem thêm:

TỪ KHÓA LIÊN QUAN

Mục lục

    Part 1 – Getting started (30 minutes)

    Part 3 – Lab Assignment (60 minutes)

    Part 4 – Do it your self

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w