Tải bản đầy đủ (.docx) (5 trang)

C lab08

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (87.52 KB, 5 trang )

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 1 – Getting started (30 minutes)
1. Using generic class
Following an application has two class Product and Program.
Class Product has 3 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.
1. 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);
}
}

2. Create class Program
class Program
{
public static void Main()
{
List<Product> inv = new List<Product>();
// 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);
}
}
}
2. Creating generic class
This application create 3 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.
1. Class Pair
class Pair<T>
{
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; }
}
}
2.

Class Program
class Program
{
public static void Main(string[] args)
{
Pair<String> pair = new Pair<string>("An","Nga");
Console.WriteLine("({0},{1})", pair.First,
pair.Second);
Console.ReadLine();
}
}
3. 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<String>(ref a, ref b);
System.Console.WriteLine(a + " " + b);
}
static void Swap<T>(ref T lhs, ref T rhs)

{
T temp = lhs;
lhs = rhs;
rhs = temp;
}
}
4. 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<int> list = new List<int>();
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<int> list)
{

if (list==null || list.Count==0)
{
return null;
}
return list[0];
}
}

Part 2 – 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 3 – 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 4 – 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 min.
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 min. Then call
the method on an array of type double[].

©2011- FPT Aptech

Page 5/4



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×