Tạo bởi Trắc Nghiệm Việt|
[Share Code] Tìm hiểu event + delegate + Collection - Lập trình C#
C Sharp
[Share Code] Tìm hiểu event + delegate + Collection - Lập trình C#
using System;
namespace Lesson06
{
[Serializable]
public class ClassRoom
{
public string Name { get; set; }
public string Code { get; set; }
public ClassRoom()
{
}
public ClassRoom(string name, string code)
{
this.Name = name;
this.Code = code;
}
public void Display()
{
Console.WriteLine("Name: {0}, Code: {1}", Name, Code);
}
}
}
using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
namespace Lesson06
{
public delegate float Calculator(float x, float y);
class MainClass
{
static event Calculator calEvent = null;
public static void Main(string[] args)
{
List<ClassRoom> list = new List<ClassRoom>();
list.Add(new ClassRoom("A", "A"));
list.Add(new ClassRoom("C", "C"));
list.Add(new ClassRoom("B", "B"));
list.Add(new ClassRoom("E", "E"));
list.Add(new ClassRoom("A", "A"));
list.Sort((o1, o2) =>
{
return o1.Code.CompareTo(o2.Code);
});
foreach(ClassRoom c in list)
{
c.Display();
}
//Luu List object vao file.
string filePath = "classroom.dat";
using (Stream stream = File.Open(filePath, FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
//foreach(ClassRoom c in list)
//{
// binaryFormatter.Serialize(stream, c);
//}
binaryFormatter.Serialize(stream, list);
}
List<ClassRoom> list2 = new List<ClassRoom>();
using (Stream stream = File.Open(filePath, FileMode.Open))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
list2 = (List<ClassRoom>)binaryFormatter.Deserialize(stream);
}
foreach(ClassRoom c in list2)
{
c.Display();
}
}
static void Test()
{
Console.WriteLine("Hello World!");
float x = 6.2f, y = 7.8f;
float r1 = Add(x, y);
float r2 = Sub(x, y);
Console.WriteLine("r1 = {0}, r2 = {1}", r1, r2);
//Bai 1: Tao 1 list -> Quan ly duoc cac function Add, Sub => duyet qua phan tu trong mang -> thuc thi lenh
//Yeu cau: tat ca cac function -> cung kieu tham
List<Calculator> list = new List<Calculator>();
Calculator c1 = new Calculator(Add);
//Calculator c2 = new Calculator(Sub);
list.Add(c1);
//list.Add(c2);
list.Add(Sub);
Calculator c3 = delegate (float x1, float y1) {
return x1 * y1;
};
list.Add(c3);
foreach (Calculator c in list)
{
Console.WriteLine("Ket qua: {0}", c(x, y));
}
//Chuc nang kha moi so vs Java (PHP).
Calculator cal = Add;
cal += Sub;
cal += delegate (float x1, float y1) {
Console.WriteLine("Tich: {0}", x * y);
return x * y;
};
cal(x, y);
//Gia lap event trong C#
calEvent = Add;
calEvent(x, y);
//List
ClassRoom classRoom = new ClassRoom("T2008A ABC", "T2008A");
Hashtable hashtable = new Hashtable();
hashtable.Add("fullname", "TRAN VAN DIEP");
hashtable.Add("age", 32);
hashtable.Add(classRoom.Code, classRoom);
string name = (string)hashtable["fullname"];
Console.WriteLine("fullname: {0}", name);
ClassRoom cr = (ClassRoom)hashtable["T2008"];
if (cr != null)
{
cr.Display();
}
//Nghiep vu: Xay dung chuong trinh quan ly thong tin sinh vien => tim kiem thong tin sinh vien theo rollno.
//Hashtable quan ly du lieu => rollNo lam key => value: object sinh vien
}
public static float Add(float x, float y)
{
Console.WriteLine("Tong: {0}", x + y);
return x + y;
}
public static float Sub(float x, float y)
{
Console.WriteLine("Hieu: {0}", x - y);
return x - y;
}
}
}