Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về phương thức nặc danh trong C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="Hoàng Xuân Bách" data-source="post: 181237" data-attributes="member: 315148"><p><span style="font-size: 18px">Phương thức nặc danh (Anonymous method) và Biểu thức Lambda (Lambda expression) trong C# 3.0</span></p><p><span style="font-size: 18px"><strong><em>Phương thức nặc danh (Anonymous method)</em></strong></span></p><p><span style="font-size: 18px">Trong phiên bản trước của C# 2.0, ta chỉ có thể khai báo delegate tham chiếu đến phương thức đặt tên (named method). C# 3.0 giới thiệu phương thức nặc danh (anonymous method) cho phép ta khi khởi tạo đối tượng delegate, không cần tách riêng phương thức mà delegate tham chiếu đến như ví dụ sau:</span></p><p><span style="font-size: 18px">// Khai báo delegate</span></p><p><span style="font-size: 18px">public delegate void SimpleDelegate(int x);</span></p><p><span style="font-size: 18px">// Khởi tạo delegate sử dụng phương thức nặc danh</span></p><p><span style="font-size: 18px">SimpleDelegate simpleDelegate = delegate(int n)</span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> Console.WriteLine("Phương thức được gọi bởi delegate");</span></p><p><span style="font-size: 18px"> Console.WriteLine<img src="https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/72x72/1f44e.png" class="smilie smilie--emoji" loading="lazy" width="72" height="72" alt="(n)" title="Thumbs down (n)" data-smilie="23"data-shortname="(n)" />;</span></p><p></p><p><span style="font-size: 18px">};</span></p><p><span style="font-size: 18px">// Gọi delegate</span></p><p><span style="font-size: 18px">simpleDelegate(2);</span></p><p></p><p><span style="font-size: 18px"> <strong><em>Biểu thức Lambda (Lambda expression)</em></strong></span></p><p><span style="font-size: 18px"> C# 3.0 đưa ra khái niệm biểu thức Lambda (Lambda expression). Biểu thức Lambda là một hàm nặc danh (anonymous function) chứa biểu thức hay khối lệnh, có thể sử dụng để tạo delegate hay cây biểu thức (expression tree). Biểu thức Lambda sử dụng phép toán Lambda =>. Vế trái phép toán Lambda là các tham đối đầu vào, và vế phải là biểu thức hay khối lệnh:</span></p><p><span style="font-size: 18px"> (<em>DanhSáchThamĐối</em>) => <em>BiểuThức/ KhốiLệnh</em></span></p><p><span style="font-size: 18px"> Nếu không có tham đối thì cú pháp sẽ như sau:</span></p><p><span style="font-size: 18px"> () => <em>BiểuThức/ KhốiLệnh</em></span></p><p><span style="font-size: 18px"> Biểu thức Lambda: x => x * x có thể được gán đến một đối tượng delegate như sau:</span></p><p><span style="font-size: 18px">// Khai báo delegate</span></p><p><span style="font-size: 18px">public delegate int SimpleDelegate(int x);</span></p><p><span style="font-size: 18px">// Khởi tạo delegate sử dụng biểu thức Lambda</span></p><p><span style="font-size: 18px">SimpleDelegate simpleDelegate = x => x * x;</span></p><p><span style="font-size: 18px">// Gọi delegate</span></p><p><span style="font-size: 18px">int y = simpleDelegate(2);</span></p><p><span style="font-size: 18px">Console.WriteLine;</span></p><p></p><p><span style="font-size: 18px"> Hay biểu thức lambda chứa khối lệnh có thể gán đến đối tượng delegate như sau:</span></p><p><span style="font-size: 18px">// Khai báo delegate</span></p><p><span style="font-size: 18px">public delegate void SimpleDelegate(int x, int y);</span></p><p><span style="font-size: 18px">// Khởi tạo delegate sử dụng biểu thức Lambda</span></p><p><span style="font-size: 18px">SimpleDelegate simpleDelegate = (x, y) =></span></p><p><span style="font-size: 18px">{</span></p><p><span style="font-size: 18px"> Console.WriteLine(x);</span></p><p><span style="font-size: 18px"> Console.WriteLine;</span></p><p><span style="font-size: 18px">};</span></p><p><span style="font-size: 18px">// Gọi delegate</span></p><p><span style="font-size: 18px">simpleDelegate(2,3);</span></p></blockquote><p></p>
[QUOTE="Hoàng Xuân Bách, post: 181237, member: 315148"] [SIZE=5]Phương thức nặc danh (Anonymous method) và Biểu thức Lambda (Lambda expression) trong C# 3.0 [B][I]Phương thức nặc danh (Anonymous method)[/I][/B] Trong phiên bản trước của C# 2.0, ta chỉ có thể khai báo delegate tham chiếu đến phương thức đặt tên (named method). C# 3.0 giới thiệu phương thức nặc danh (anonymous method) cho phép ta khi khởi tạo đối tượng delegate, không cần tách riêng phương thức mà delegate tham chiếu đến như ví dụ sau: // Khai báo delegate public delegate void SimpleDelegate(int x); // Khởi tạo delegate sử dụng phương thức nặc danh SimpleDelegate simpleDelegate = delegate(int n) { Console.WriteLine("Phương thức được gọi bởi delegate"); Console.WriteLine(n);[/SIZE] [SIZE=5]}; // Gọi delegate simpleDelegate(2);[/SIZE] [SIZE=5] [B][I]Biểu thức Lambda (Lambda expression)[/I][/B] C# 3.0 đưa ra khái niệm biểu thức Lambda (Lambda expression). Biểu thức Lambda là một hàm nặc danh (anonymous function) chứa biểu thức hay khối lệnh, có thể sử dụng để tạo delegate hay cây biểu thức (expression tree). Biểu thức Lambda sử dụng phép toán Lambda =>. Vế trái phép toán Lambda là các tham đối đầu vào, và vế phải là biểu thức hay khối lệnh: ([I]DanhSáchThamĐối[/I]) => [I]BiểuThức/ KhốiLệnh[/I] Nếu không có tham đối thì cú pháp sẽ như sau: () => [I]BiểuThức/ KhốiLệnh[/I] Biểu thức Lambda: x => x * x có thể được gán đến một đối tượng delegate như sau: // Khai báo delegate public delegate int SimpleDelegate(int x); // Khởi tạo delegate sử dụng biểu thức Lambda SimpleDelegate simpleDelegate = x => x * x; // Gọi delegate int y = simpleDelegate(2); Console.WriteLine;[/SIZE] [SIZE=5] Hay biểu thức lambda chứa khối lệnh có thể gán đến đối tượng delegate như sau: // Khai báo delegate public delegate void SimpleDelegate(int x, int y); // Khởi tạo delegate sử dụng biểu thức Lambda SimpleDelegate simpleDelegate = (x, y) => { Console.WriteLine(x); Console.WriteLine; }; // Gọi delegate simpleDelegate(2,3);[/SIZE] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về phương thức nặc danh trong C#
Top