By GokiSoft.com| 15:31 07/10/2021|
C Sharp

[Video] Tạo khung dự án C Sharp + Tìm hiểu alias namespace C Sharp + Event & Delegate C Sharp

Link Video Bai Giang

Tóm tắt nội dung học C# | C Sharp:

Plan:
Thiết kế database:
=========================================================

Xây dựng cấu trúc chương trình - Phát triển dự án:

Chương trình quản lý trường học cho Aptech:
- Quản lý phòng học
- Quản lý lớp học
- Quản lý sinh viên
- Quản lý kỳ học
- Quản lý môn học
- Quản lý điểm thi
...

- Phân tích qua cấu trúc dự án:

-> Làm việc vs database
-> Thư viên -> tự code -> sử dụng chung cho toàn dự án

Xây dựng module chương trình.

Tables <-> Class Object

==========================================================
Xây dựng 2 Exception đặt tên là: NegativeException, NotRectangleException -> Kế thừa từ class Exception
Xây dựng 1 class object -> Rectangle gồm 3 canh double a, b, c -> Tạo 1 hàm tạo 3 tham số a,b,c -> cài dữ liệu
Yêu cầu: Hàm tạo trên cần check các điều kiện sau
	- a,b,c < 0 -> throw NegativeException
	- tổng 2 canh < 1 canh -> throw NotRectangleException
Test:
	Tạo 1 object từ Rectangle
		- Cạnh của 1 tam giác luôn > 0: TH a,b,c <= 0
		- Không phải 3 cạnh của 1 tam giác: TH tổng 2 canh < 1 canh
		- Khỏi tạo thành công


Tạo khung chương trình C# & khung dự án C#


Tìm hiểu exception trong C# & Exception C Sharp

#NotRectangleException.cs


using System;
namespace Lesson05
{
    public class NotRectangleException : Exception
    {
        public NotRectangleException()
        {
        }
    }
}


#NegativeException.cs


using System;
namespace Lesson05
{
    public class NegativeException : Exception
    {
        public NegativeException()
        {
        }
    }
}


#Rectangle.cs


using System;
namespace Lesson05
{
    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 NegativeException();
            }

            if((a + b < c) || (a + c < b) || (b + c) < a)
            {
                throw new NotRectangleException();
            }

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

CHƯƠNG TRÌNH TEST EXCEPTION
try
            {
                Rectangle rectangle = new Rectangle(1, 2, 2);
                Console.WriteLine("Khoi tao object thanh cong");
            }
            catch (NegativeException e)
            {
                Console.WriteLine("Yeu cau canh cua tam giac > 0");
            }
            catch (NotRectangleException e)
            {
                Console.WriteLine("Khong phai 3 canh cua 1 tam giac");
            }

Tìm hiểu Exception C# & Exception C Sharp


//Test exception
            int x = 0;
            Console.WriteLine("Nhap x = ");

            try
            {
                x = int.Parse(Console.ReadLine());
                Console.WriteLine("x = " + x);
            }
            catch (FormatException ex)
            {
                Console.WriteLine("Nhap sai!!!");
            }

            int y = 0;
            try
            {
                y = int.Parse(Console.ReadLine());
                Console.WriteLine("y = " + y);
            }
            catch (FormatException ex)
            {
                Console.WriteLine("Nhap sai!!!");
            }

            try
            {
                int z = x / y;
                Console.WriteLine("z = " + z);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error devide by zero!!!");
            }

            if (y == 0)
            {
                Console.WriteLine("Error devide by zero!!!");
            }
            else
            {
                int z = x / y;
                Console.WriteLine("z = " + z);
            }



Delegate C# & Event C# & Delegate C Sharp & Event C Sharp

#Program.cs


using System;
using Lesson05.Modules.Student;
using Sem = Lesson05.Modules.Semester;

namespace Lesson05
{
    delegate void OnRunning();

    delegate double Calculator(double a, double b);

    class Program
    {
        static event Calculator tinhtongEvent;

        static void Main(string[] args)
        {
            //Test01();
            //Test02();
            Test03();
        }

        static void Test03()
        {
            //Test anonymous class trong C#
            //Test lambda trong C#
            People p = new People();
            p.OnRunning();

            //delegate
            OnRunning running = delegate {
                Console.WriteLine("Test....");
	        };
            running();

            OnRunning running1 = delegate() {
                Console.WriteLine("Test 2....");
            };

            OnRunning running2 = () =>
            {
                Console.WriteLine("Test 3 .....");
            };

            Calculator cal1 = delegate(double a, double b) {
                return a + b;
	        };
            double tong = cal1(2, 6);
            Console.WriteLine("tong: " + tong);

            Calculator cal2 = (a, b) => {
                return a - b;
            };

            cal2 = (double a, double b) => {
                return a - b;
            };
            double hieu = cal2(2, 6);
            Console.WriteLine("tong: " + hieu);

            //Diem dac biet trong delegate nhu sau
            Calculator calculator = (a, b) => {
                Console.WriteLine("Tinh hieu ...");
                return a - b;
            };
            //calculator -> tinh hieu
            calculator += (x, y) =>
            {
                Console.WriteLine("Tinh tich ...");
                return x * y;
            };
            //calculator -> quan ly 2 method: tinh hieu + tinh tich

            calculator += TinhTong;
            //calculator -> quan ly 3 method: tinh hieu + tinh tich + tinhtong
            calculator += new Calculator(TinhTong);
            //calculator -> quan ly 4 method: tinh hieu + tinh tich + 2 * tinhtong

            calculator(2, 6);
            //tinhhieu(2,6), tinhtich(2,6), tinhtong(2,6), tinhtong(2,6)

            double k = calculator(2, 6);
            //tinhhieu(2,6), tinhtich(2,6), tinhtong(2,6), tinhtong(2,6) -> k
            Console.WriteLine("k = " + k);

            //Event
            //cach viet 1
            tinhtongEvent = TinhTong;
            //cach viet 2
            tinhtongEvent = new Calculator(TinhTong);

            tinhtongEvent += (a, b) =>
            {
                Console.WriteLine("Tich: {0}", a * b);
                return a * b;
            };

            tinhtongEvent(8, 2);
        }

        static double TinhTong(double a, double b)
        {
            Console.WriteLine("Tong: " + (a + b));
            return a + b;
        }

        static void Test02()
        {
            try
            {
                Rectangle rectangle = new Rectangle(1, 2, 2);
                Console.WriteLine("Khoi tao object thanh cong");
            }
            catch (NegativeException e)
            {
                Console.WriteLine("Yeu cau canh cua tam giac > 0");
            }
            catch (NotRectangleException e)
            {
                Console.WriteLine("Khong phai 3 canh cua 1 tam giac");
            }
        }

        static void Test01()
        {
            Console.WriteLine("Hello World!");
            Lesson05.Modules.Student.Config config = new Lesson05.Modules.Student.Config();
            config.ShowMenu();

            Sem.Config config1 = new Sem.Config();
            config1.ShowTest();

            Sem.Test test = new Sem.Test();
            test.ShowAbc();

            //Config c = null;
            //c.ShowMenu();

            //Test exception
            int x = 0;
            Console.WriteLine("Nhap x = ");

            try
            {
                x = int.Parse(Console.ReadLine());
                Console.WriteLine("x = " + x);
            }
            catch (FormatException ex)
            {
                Console.WriteLine("Nhap sai!!!");
            }

            int y = 0;
            try
            {
                y = int.Parse(Console.ReadLine());
                Console.WriteLine("y = " + y);
            }
            catch (FormatException ex)
            {
                Console.WriteLine("Nhap sai!!!");
            }

            try
            {
                int z = x / y;
                Console.WriteLine("z = " + z);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error devide by zero!!!");
            }

            if (y == 0)
            {
                Console.WriteLine("Error devide by zero!!!");
            }
            else
            {
                int z = x / y;
                Console.WriteLine("z = " + z);
            }
        }
    }
}

//AA -> ClassA
//AA.BB -> ClassB
//AA.BB.CC -> ClassC

//A -> ClassA
//A.B -> ClassB
//A.B.C -> ClassC

namespace A
{
    class ClassA
    {

    }

    namespace B
    {
        class ClassB
        {

        }

        namespace C
        {
            class ClassC
            {

            }
        }
    }
}



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó