1. Trang chủ
  2. » Thể loại khác

csharp anonymous methods

3 87 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Phương thức nặc danh trong C#

    • Viết một Phương thức nặc danh trong C#

    • Ví dụ

Nội dung

http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Phương thức nặc danh C# Chúng ta bàn luận Delegate sử dụng để tham chiếu phương thức mà có dấu hiệu Delegate Nói cách khác, bạn gọi phương thức mà tham chiếu Delegate sử dụng đối tượng Delegate Phương thức nặc danh (Anonymous Method) C# cung cấp kỹ thuật để truyền khối code tham số delegate Các phương thức nặc danh phương thức khơng có tên, có thân phương thức Bạn khơng cần xác định kiểu trả phương thức nặc danh; suy từ lệnh return bên thân phương thức nặc danh Viết Phương thức nặc danh C# Các phương thức nặc danh (Anonymous Method) C# khai báo với việc tạo instance Delegate đó, với từ khóa delegate Ví dụ: delegate void NumberChanger(int n); NumberChanger nc = delegate(int x) { Console.WriteLine("Anonymous Method: {0}", x); }; Khối Console.WriteLine("Anonymous Method: {0}", x); phần thân phương thức nặc danh Delegate gọi với phương thức nặc danh phương thức đặt tên theo cách, ví dụ: việc truyền tham số phương thức tới đối tượng Delegate Ví dụ: nc(10); Ví dụ Dưới ví dụ minh họa khái niệm trên: using System; http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com delegate void NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static void AddNum(int p) { num += p; Console.WriteLine("Named Method: {0}", num); } public static void MultNum(int q) { num *= q; Console.WriteLine("Named Method: {0}", num); } public static int getNum() { return num; } static void Main(string[] args) { //create delegate instances using anonymous method NumberChanger nc = delegate(int x) { Console.WriteLine("Anonymous Method: {0}", x); }; //calling the delegate using the anonymous method nc(10); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com //instantiating the delegate using the named methods nc = new NumberChanger(AddNum); //calling the delegate using the named methods nc(5); //instantiating the delegate using another named methods nc = new NumberChanger(MultNum); //calling the delegate using the named methods nc(2); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Anonymous Method: 10 Named Method: 15 Named Method: 30 http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... //create delegate instances using anonymous method NumberChanger nc = delegate(int x) { Console.WriteLine( "Anonymous Method: {0}", x); }; //calling the delegate using the anonymous method nc(10); http://vietjack.com/... http://vietjack.com /csharp/ index.jsp Copyright © vietjack.com //instantiating the delegate using the named methods nc = new NumberChanger(AddNum); //calling the delegate using the named methods nc(5);... another named methods nc = new NumberChanger(MultNum); //calling the delegate using the named methods nc(2); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Anonymous Method:

Ngày đăng: 02/12/2017, 19:59

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w