By GokiSoft.com|
15:54 05/10/2021|
C Sharp
[Video] Tìm hiểu về delegate & event + Stack & Queue & Collection + Generic + Partial trong C# - Khoá học lập trình C Sharp
Link Video Bài Giảng
#Student02.cs
using System;
namespace Lesson06
{
public partial class Student
{
public string Address { get; set; }
public Student(string fullname, string rollno, string address)
{
Fullname = fullname;
RollNo = rollno;
Address = address;
}
public void Display()
{
Console.WriteLine("Ten: {0}, MSV: {1}, Dia Chi: {2}", Fullname, RollNo, Address);
}
}
}
#Student01.cs
using System;
namespace Lesson06
{
public partial class Student
{
public string Fullname { get; set; }
public string RollNo { get; set; }
public Student()
{
}
public void Input()
{
Console.WriteLine("Nhap ten: ");
Fullname = Console.ReadLine();
Console.WriteLine("Nhap rollno: ");
RollNo = Console.ReadLine();
Console.WriteLine("Nhap dia chi: ");
Address = Console.ReadLine();
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Lesson06
{
public delegate void OnRunning();
/**
* Cong, Tru, Nhan, Chia, .v.v
*/
public delegate double Calculator(double x, double y);
class Program
{
static event Calculator HieuEvent;
static void Main(string[] args)
{
//Test01();
//Test02();
//Test03();
Test04();
}
static void Test04()
{
Student student = new Student();
student.Input();
Console.WriteLine("Ten: " + student.Fullname);
Console.WriteLine("DC: " + student.Address);
}
static void Test03()
{
List<int> list1 = new List<int>();
list1.Add(1);
list1.Add(5);
list1.Add(2);
int k = list1[0];
//string v2 = list[1]; Error
Console.WriteLine("k = " + k);
List<string> list2 = new List<string>();
list2.Add("Vi du 1");
list2.Add("Vi du 7");
list2.Add("Vi du 2");
string v = list2[1];
//int v3 = list[0];Error
Console.WriteLine("v = " + v);
//Cau hoi 1: List<int> & List<string> -> bao nhieu class object -> chi la 1 class object chung thoi
//Khac nhau kieu du lieu truyen vao int, string, double, char, float, other class.
//Generic -> ??? -> Truyen kieu du lieu int, string, ... => Luc su dung khoi tao object tu class object.
}
static void Test02()
{
//Stack -> LIFO (Last In First Out)
//Khai bao mang -> Tuong tuong cach quan ly -> trong sach
Stack<string> list = new Stack<string>();
//Them phan tu vao
Console.WriteLine("So phan tu trong stack: " + list.Count);
list.Push("Vi du 1");
Console.WriteLine("So phan tu trong stack: " + list.Count);
list.Push("Vi du 2");
Console.WriteLine("So phan tu trong stack: " + list.Count);
list.Push("Vi du 3");
Console.WriteLine("So phan tu trong stack: " + list.Count);
//Lay phan tu ra
string v = list.Pop();
Console.WriteLine("v: " + v);
Console.WriteLine("So phan tu trong stack: " + list.Count);
v = list.Pop();
Console.WriteLine("v: " + v);
Console.WriteLine("So phan tu trong stack: " + list.Count);
//Chu y: Stack & Queue -> Push & Pop
//Queue -> FIFO (First In First Out)
//Khai bao mang -> Tuong tuong cach quan ly -> xep hang mua ve xem phim
Queue<string> queue = new Queue<string>();
//Them phan tu vao
Console.WriteLine("So phan tu trong stack: " + queue.Count);
queue.Enqueue("Vi du 1");
Console.WriteLine("So phan tu trong stack: " + queue.Count);
queue.Enqueue("Vi du 2");
Console.WriteLine("So phan tu trong stack: " + queue.Count);
queue.Enqueue("Vi du 3");
Console.WriteLine("So phan tu trong stack: " + queue.Count);
//Lay phan tu ra
v = queue.Dequeue();
Console.WriteLine("v: " + v);
Console.WriteLine("So phan tu trong stack: " + queue.Count);
v = queue.Dequeue();
Console.WriteLine("v: " + v);
Console.WriteLine("So phan tu trong stack: " + queue.Count);
}
static void Test01()
{
People p = new People();
p.onRunning();
//Anonymous -> Code chi su dung 1 lan -> ko tai su dung
OnRunning running = delegate () {
Console.WriteLine("Test delegate ...");
};
running();
double tong = Cong(1.2F, 6.9F);
Console.WriteLine("Tong: " + tong);
//Anonymous -> Code chi su dung 1 lan -> ko tai su dung
Calculator calTru = delegate (double x, double y) {
return x - y;
};
double hieu = calTru(5.6F, 2.6F);
Console.WriteLine("Hieu: " + hieu);
//Diem manh delegate -> ket hop nhieu function (tuong + anonymous) -> chay dong thoi dc
Calculator cal = Hieu;
//Anonymous -> T1
cal += delegate (double x, double y) {
Console.WriteLine("Tich: " + (x * y));
return x * y;
};
//Anonymous -> T2
cal += delegate (double x, double y) {
Console.WriteLine("Thuong: " + (x / y));
return x / y;
};
cal(6, 2);
//Hieu(6,2), T1(6,2), T2(6,2)
//TH -> void
//Test Event
HieuEvent = Hieu;
//HieuEvent += Cong;
//HieuEvent = new Calculator(Hieu);
HieuEvent(6.2F, 1.2F);
}
static double Cong(double x, double y)
{
Console.WriteLine("Tong: " + (x + y));
return x + y;
}
static double Hieu(double x, double y)
{
Console.WriteLine("Hieu: " + (x - y));
return x - y;
}
}
}
#People.cs
using System;
namespace Lesson06
{
public class People : IRunning
{
public People()
{
}
public void onRunning()
{
Console.WriteLine("People is running...");
}
}
}
#IRunning.cs
using System;
namespace Lesson06
{
public interface IRunning
{
void onRunning();
}
}
#IAction.cs
using System;
namespace Lesson06
{
public interface IAction
{
void onRunning();
void onSleeping();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)