By GokiSoft.com|
14:41 26/05/2021|
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;
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-28 15:54:16
using System;
using System.Collections.Generic;
using System.IO;
namespace Lesson7.BookProject
{
class Program
{
static List<AptechBook> BookAptechList = new List<AptechBook>();
static void Main(string[] args)
{
int choose;
do
{
Menu();
choose = Int32.Parse(Console.ReadLine());
switch (choose)
{
case 1:
InputBook();
break;
case 2:
DisplayAllBook();
break;
case 3:
BookAptechList.Sort((o1, o2) =>
{
return -o1.YearPublishing.CompareTo(o2.YearPublishing);
});
foreach (AptechBook aptechbook in BookAptechList)
{
aptechbook.Display();
}
break;
case 4:
FindBookByBookName();
break;
case 5:
FindBookByAuthorName();
break;
case 6:
SaveInFile();
break;
case 7:
ReadFile();
break;
case 8:
Console.WriteLine("Thoat...");
break;
}
} while (choose!=8);
}
private static void ReadFile()
{
using (Stream stream = File.Open("Books.dat", FileMode.Open))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
BookAptechList = (List<AptechBook>)binaryFormatter.Deserialize(stream);
}
Console.WriteLine("Doc file thanh cong");
}
private static void SaveInFile()
{
string filePath = "Books.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, BookAptechList);
}
Console.WriteLine("Luu file thanh cong");
}
private static void FindBookByBookName()
{
Console.WriteLine("Nhap ten sach can tim:");
string FindBookName = Console.ReadLine();
foreach(AptechBook aptechbook in BookAptechList)
{
if(aptechbook.BookName == FindBookName)
{
aptechbook.Display();
}
}
}
private static void FindBookByAuthorName()
{
Console.WriteLine("Nhap ten sach can tim:");
string FindAuthorName = Console.ReadLine();
foreach (AptechBook aptechbook in BookAptechList)
{
if (aptechbook.BookAuthor == FindAuthorName)
{
aptechbook.Display();
}
}
}
private static void InputBook()
{
Console.WriteLine("Nhap so luong cuon sach muon nhap:");
int N= Int32.Parse(Console.ReadLine());
for(int i =0 ; i < N; i++)
{
AptechBook aptechbook = new AptechBook();
aptechbook.Input();
BookAptechList.Add(aptechbook);
}
}
private static void DisplayAllBook()
{
for(int i = 0 ; i < BookAptechList.Count ; i++)
{
BookAptechList[i].Display();
}
}
public static void Menu()
{
Console.WriteLine("1.Nhập thông tin n cuốn sách của Aptech");
Console.WriteLine("2.Hiển thị thông tin vừa nhập");
Console.WriteLine("3.Sắp xếp thông tin giảm dần theo năm xuất bản và hiển thị");
Console.WriteLine("4.Tìm kiếm theo tên sách");
Console.WriteLine("5.Tìm kiếm theo tên tác giả");
Console.WriteLine("6.Lưu thông tin sách đã nhập vào file");
Console.WriteLine("7. Đọc nội dung từ file và lưu vào mang quản lý sách");
Console.WriteLine("8. Thoat");
Console.WriteLine("Chon: ");
}
}
}