By GokiSoft.com| 08:36 19/10/2021|
C Sharp

[Video] Tìm hiểu Exception C Sharp, namespace C Sharp, Delegate C Sharp, Events C#, Collection C#, Stack C#, Queue C#


Link Video Bai Giang


Tìm hiểu Exception trong C#

Tạo ra 2 exception đặt tên là: NegativeNumberException, NotRectangleException kế thừa từ class Exception
Tạo ra 1 class: Rectangle gồm 2 cạnh a, b, c -> hàm tạo đầy đủ đối số và check điều kiện sau
a, b, c < 0 -> văng ra error: NegativeNumberException
tổng 2 cạnh < cạnh còn lại -> văng ra error: NotRectangleException

Viết chương trình test: Tạo đối tượng Rectangle
	- Cạnh của tam giác ko được âm -> 1 trong các tham số có số âm
	- 2 cạnh của tam giác phải lớn hơn cạnh còn lại -> NotRectangleException
	- Khởi tạo đối tượng thành công.




#Rectangle.cs


using System;
namespace Lesson06.Exc
{
    public class Rectangle
    {
        public double A { get; set; }
        public double B { get; set; }
        public double C { get; set; }

        public Rectangle()
        {
        }

        public Rectangle(double a, double b, double c)
        {
            if(a < 0 || b < 0 || c < 0) {
                throw new NegativeNumberException();
            }
            if((a + b < c) || (a + c < b) || (b + c < a))
            {
                throw new NotRectangleException();
            }

            A = a;
            B = b;
            C = c;
        }
    }
}


#NotRectangleException.cs


using System;
namespace Lesson06.Exc
{
    public class NotRectangleException : Exception
    {
        public NotRectangleException()
        {
        }
    }
}


#NegativeNumberException.cs


using System;
namespace Lesson06.Exc
{
    public class NegativeNumberException : Exception
    {
        public NegativeNumberException()
        {
        }
    }
}

Test chức năng Exception
static void Test02()
        {
            int x = 0, y = 0;
            try
            {
                Console.WriteLine("Nhap x = ");
                x = int.Parse(Console.ReadLine());
            }
            catch { }

            try
            {
                Console.WriteLine("Nhap y = ");
                y = int.Parse(Console.ReadLine());
            }
            catch (Exception e) { }

            try
            {
                int s = x / y;
                Console.WriteLine("s = " + s);
            }
            catch { }
            finally
            {
                Console.WriteLine("... giai phong tai nguyen ...");
            }

            if(y == 0) {
                Console.WriteLine("Error chia cho 0");
            } else
            {
                int ss = x / y;
                Console.WriteLine("ss = " + ss);
            }

            Rectangle rectangle;

            //NegativeNumberException
            try
            {
                rectangle = new Rectangle(-2, 0, 10);
                Console.WriteLine("Khởi tạo đối tượng thành công.");
            } catch(NegativeNumberException e)
            {
                Console.WriteLine("Cạnh của tam giác ko được âm");
            } catch(NotRectangleException e)
            {
                Console.WriteLine("2 cạnh của tam giác phải lớn hơn cạnh còn lại");
            }

            //NotRectangleException
            try
            {
                rectangle = new Rectangle(2, 5, 10);
                Console.WriteLine("Khởi tạo đối tượng thành công.");
            }
            catch (NegativeNumberException e)
            {
                Console.WriteLine("Cạnh của tam giác ko được âm");
            }
            catch (NotRectangleException e)
            {
                Console.WriteLine("2 cạnh của tam giác phải lớn hơn cạnh còn lại");
            }


            //Thanh cong
            try
            {
                rectangle = new Rectangle(2, 5, 7);
                Console.WriteLine("Khởi tạo đối tượng thành công.");
            }
            catch (NegativeNumberException e)
            {
                Console.WriteLine("Cạnh của tam giác ko được âm");
            }
            catch (NotRectangleException e)
            {
                Console.WriteLine("2 cạnh của tam giác phải lớn hơn cạnh còn lại");
            }
        }

Ví dụ tạo khung dự án


Tìm hiểu Delegate + Events + Collections


#Program.cs


using System;
using S1 = Lesson06.Sub;
using S2 = Lesson06.Sub.OKOK.Ahhh;
using Lesson06.Exc;
using System.Collections.Generic;
using System.Collections;

namespace Lesson06
{
    interface IRunning
    {
        void OnRunning();
    }

    class Car : IRunning
    {
        public void OnRunning()
        {
            Console.WriteLine("Car is running ...");
        }
    }

    delegate void OnRunning();
    delegate double Calculator(double x, double y);

    class Program
    {
        static void Main(string[] args)
        {
            //Test01();
            //Xay dung 1 khung chuong trinh -> namespace <-> folder trong du
            //Modules ->

            //Exception
            //Test02();

            //Delegate & Event
            //Test03();

            //Collections
            Test04();
        }

        static void Test04()
        {
            //List, ArrayList, Dictionary (HashMap, Hashtable), Stack, Queue
            //List
            List<string> list1 = new List<string>();
            list1.Add("asdasd");
            list1.Add("sdfdf2343");

            list1.RemoveAt(0);

            Console.WriteLine("list1[0] = " + list1[0]);

            List<Object> list2 = new List<Object>();
            list2.Add("12313");
            list2.Add(324);
            list2.Add(true);

            //ArrayList
            ArrayList arrayList = new ArrayList();
            arrayList.Add("12313");
            arrayList.Add(324);
            arrayList.Add(true);

            //Dictionary
            Dictionary<string, string> dic = new Dictionary<string, string>();
            //dic.Add("fullname", "Tran Van A");
            //dic.Add("age", "32");
            dic["age"] = "32";
            dic["fullname"] = "Tran Van B";

            string v = dic["fullname"];
            Console.WriteLine("v = " + v);

            //Stack
            Stack<string> stack = new Stack<string>();
            Console.WriteLine("==================");
            stack.Push("1");
            foreach(string v1 in stack)
            {
                Console.WriteLine(v1);
            }
            Console.WriteLine("==================");
            stack.Push("2");
            foreach (string v1 in stack)
            {
                Console.WriteLine(v1);
            }
            Console.WriteLine("==================");
            stack.Push("3");
            foreach (string v1 in stack)
            {
                Console.WriteLine(v1);
            }
            Console.WriteLine("==================");
            v = stack.Pop();
            Console.WriteLine("pop: " + v);
            foreach (string v1 in stack)
            {
                Console.WriteLine(v1);
            }

            //Queue
            Queue<string> queue = new Queue<string>();
            Console.WriteLine("==================");
            queue.Enqueue("1");
            foreach (string v1 in queue)
            {
                Console.WriteLine(v1);
            }
            Console.WriteLine("==================");
            queue.Enqueue("2");
            foreach (string v1 in queue)
            {
                Console.WriteLine(v1);
            }
            Console.WriteLine("==================");
            queue.Enqueue("3");
            foreach (string v1 in queue)
            {
                Console.WriteLine(v1);
            }
            Console.WriteLine("==================");
            v = queue.Dequeue();
            Console.WriteLine("pop: " + v);
            foreach (string v1 in queue)
            {
                Console.WriteLine(v1);
            }
        }

        static event Calculator tinhtongEvent;
        static void Test03()
        {
            //Interface + Abstract -> Khong the khoi tao dc object truc tiep tu interface + abstract class
            Car car = new Car();
            car.OnRunning();

            //Bai toan dat ra: trong C# -> thanh phan anonymous -> Co -> delegate
            //Khoi tao doi anonymous method -> delegate
            //C1:
            OnRunning run = delegate {
                Console.WriteLine("Testing ... 01 ...");
	        };
            run();

            run = delegate() {
                Console.WriteLine("Testing ... 02 ...");
            };
            run();

            run = () => {
                Console.WriteLine("Testing ... 03 ...");
            };
            run();

            run = () => Console.WriteLine("Testing ... 04 ...");
            run();

            run = new OnRunning(delegate {
                Console.WriteLine("Testing ... 05 ...");
	        });
            run();

            //Vi du 2: Calculator
            Calculator cal = (x, y) => x + y;
            double s = cal(5, 2);
            Console.WriteLine("s = " + s);

            cal = (x, y) =>
            {
                return x + y;
            };
            s = cal(5, 2);
            Console.WriteLine("s = " + s);

            cal = (double x, double y) =>
            {
                return x + y;
            };
            s = cal(5, 2);
            Console.WriteLine("s = " + s);

            //Diem dac biet cua delegate
            cal = TinhTong;
            s = cal(5, 6);
            Console.WriteLine("s = " + s);

            cal = TinhHieu;

            s = cal(5, 6);
            Console.WriteLine("s = " + s);

            //Diem khac tiep theo
            cal = TinhTong;
            cal += TinhHieu;//Hieu cal la List chua 2 phan tu TinhTong & TinhHieu
            cal(8, 2);//TinhTong(8, 2) + TinhHieu(8, 2)
            cal += new Calculator(TinhTong);
            cal += (x, y) =>
            {
                Console.WriteLine("Tinh Tich ...");
                return x * y;
            };

            cal(8, 2);//TinhTong(8, 2) + TinhHieu(8, 2) + TinhTong(8, 2) + T(8, 2)

            //Test Event
            tinhtongEvent = TinhTong;
            tinhtongEvent = new Calculator(TinhTong);

            tinhtongEvent(8, 2);
        }

        static double TinhTong(double x, double y)
        {
            Console.WriteLine("Ham tinh tong ...");
            return x + y;
        }

        static double TinhHieu(double x, double y)
        {
            Console.WriteLine("Ham tinh hieu ...");
            return x - y;
        }

        static void Test02()
        {
            int x = 0, y = 0;
            try
            {
                Console.WriteLine("Nhap x = ");
                x = int.Parse(Console.ReadLine());
            }
            catch { }

            try
            {
                Console.WriteLine("Nhap y = ");
                y = int.Parse(Console.ReadLine());
            }
            catch (Exception e) { }

            try
            {
                int s = x / y;
                Console.WriteLine("s = " + s);
            }
            catch { }
            finally
            {
                Console.WriteLine("... giai phong tai nguyen ...");
            }

            if(y == 0) {
                Console.WriteLine("Error chia cho 0");
            } else
            {
                int ss = x / y;
                Console.WriteLine("ss = " + ss);
            }

            Rectangle rectangle;

            //NegativeNumberException
            try
            {
                rectangle = new Rectangle(-2, 0, 10);
                Console.WriteLine("Khởi tạo đối tượng thành công.");
            } catch(NegativeNumberException e)
            {
                Console.WriteLine("Cạnh của tam giác ko được âm");
            } catch(NotRectangleException e)
            {
                Console.WriteLine("2 cạnh của tam giác phải lớn hơn cạnh còn lại");
            }

            //NotRectangleException
            try
            {
                rectangle = new Rectangle(2, 5, 10);
                Console.WriteLine("Khởi tạo đối tượng thành công.");
            }
            catch (NegativeNumberException e)
            {
                Console.WriteLine("Cạnh của tam giác ko được âm");
            }
            catch (NotRectangleException e)
            {
                Console.WriteLine("2 cạnh của tam giác phải lớn hơn cạnh còn lại");
            }


            //Thanh cong
            try
            {
                rectangle = new Rectangle(2, 5, 7);
                Console.WriteLine("Khởi tạo đối tượng thành công.");
            }
            catch (NegativeNumberException e)
            {
                Console.WriteLine("Cạnh của tam giác ko được âm");
            }
            catch (NotRectangleException e)
            {
                Console.WriteLine("2 cạnh của tam giác phải lớn hơn cạnh còn lại");
            }
        }

        static void Test01()
        {
            Lesson06.Sub.Test test = new Lesson06.Sub.Test();
            test.ShowMsg();

            Lesson06.Sub.OKOK.Ahhh.Test test1 = new Lesson06.Sub.OKOK.Ahhh.Test();
            test1.ShowMsg();

            S1.Test test2 = new S1.Test();
            test2.ShowMsg();

            S2.Test test3 = new S2.Test();
            test3.ShowMsg();
        }
    }
}

namespace Lesson06.Sub
{
    class Program
    {
        public void ShowTest()
        {
            Console.WriteLine("Testing...");
        }
    }

    class Test
    {
        public void ShowMsg()
        {
            Console.WriteLine("js .. 1 ...");
        }
    }
}

namespace Lesson06.Sub.OKOK.Ahhh
{
    class Program
    {
        public void ShowTest()
        {
            Console.WriteLine("Testing...");
        }
    }

    class Test
    {
        public void ShowMsg()
        {
            Console.WriteLine("js .. 2 ...");
        }
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)