By GokiSoft.com|
10:09 11/10/2021|
C Sharp
[Video] Tìm hiểu Collections C Sharp (List, ArrayList, Dictionary, Stack, Queue) - Partial & Generic C Sharp
#Student02.cs
using System;
namespace Lesson06
{
public partial class Student
{
public string RollNo { get; set; }
}
}
#Student.cs
using System;
namespace Lesson06
{
public partial class Student : IInput
{
public string Fullname { get; set; }
public Student()
{
}
public void Display()
{
Console.WriteLine("Hien thi thong tin du lieu");
}
public void Input()
{
Console.WriteLine("Nhap du lieu");
Console.WriteLine("Nhap ten: ");
Fullname = Console.ReadLine();
Console.WriteLine("Nhap ma sinh vien: ");
RollNo = Console.ReadLine();
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
using System.Collections;
namespace Lesson06
{
delegate void Input();
class Program
{
static void Main(string[] args)
{
//Test01();
//Test02();
Test03();
}
static void Test03()
{
Input input = delegate {
Console.WriteLine("Nhap du lieu");
};
input();
input = delegate() {
Console.WriteLine("Nhap du lieu");
};
input();
input = () => {
Console.WriteLine("Nhap du lieu");
};
input();
input = () => Console.WriteLine("Nhap du lieu");
input();
}
static void Test02()
{
List<int> list1 = new List<int>();
list1.Add(12);
list1.Add(15);
list1.Add(3);
list1.Add(22);
list1.Add(152);
Console.WriteLine(list1[2]);
List<string> list2 = new List<string>();
list2.Add("sadasd");
list2.Add("453534sdf");
Console.WriteLine(list2[0]);
}
/***
* Tim hieu: ArrayList & List + Dictionary + Stack| Queue
*/
static void Test01()
{
//ArrayList & List
ArrayList list = new ArrayList();
list.Add("Xin chao");
list.Add(123);
list.Add(true);
Console.WriteLine(list[0]);
List<string> t1 = new List<string>();
t1.Add("123");
t1.Add("sdsf");
Console.WriteLine(t1[0]);
Student std = new Student();
std.Display();
string str = "sdfsdfsd";
var std2 = new Student();
std2.Display();
Object obj = new Student();
List<Object> t2 = new List<object>();
t2.Add(231);
t2.Add("sdfsdf");
t2.Add(new Student());
//Stack & Queue
Stack<string> stack = new Stack<string>();
stack.Push("1"); // 1
Console.WriteLine("Phan tu trong stack");
foreach (string s in stack)
{
Console.WriteLine(s);
}
stack.Push("2"); // 1 | 2
Console.WriteLine("Phan tu trong stack");
foreach (string s in stack)
{
Console.WriteLine(s);
}
stack.Push("3"); // 1 | 2 | 3
Console.WriteLine("Phan tu trong stack");
foreach (string s in stack)
{
Console.WriteLine(s);
}
string v = stack.Pop();
Console.WriteLine("v = " + v);
Console.WriteLine("Phan tu trong stack");
foreach (string s in stack)
{
Console.WriteLine(s);
}
Queue<string> queue = new Queue<string>();
queue.Enqueue("1");
Console.WriteLine("Phan tu trong queue");
foreach (string s in queue)
{
Console.WriteLine(s);
}
queue.Enqueue("2"); // 1 | 2
Console.WriteLine("Phan tu trong queue");
foreach (string s in queue)
{
Console.WriteLine(s);
}
queue.Enqueue("3"); // 1 | 2 | 3
Console.WriteLine("Phan tu trong queue");
foreach (string s in queue)
{
Console.WriteLine(s);
}
v = queue.Dequeue();
Console.WriteLine("v = " + v);
Console.WriteLine("Phan tu trong queue");
foreach (string s in queue)
{
Console.WriteLine(s);
}
//Hashtable
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("K01", "ABskhskdfhds");
dic.Add("K02", "ABskhskd32423424fhds");
v = dic["K01"];
Console.WriteLine("v = " + v);
}
}
}
#IInput.cs
using System;
namespace Lesson06
{
public interface IInput
{
void Input();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)