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="uocmo_kchodoi" data-source="post: 177638" data-attributes="member: 165510"><p><a href="https://vnkienthuc.com/forums/code.377/" target="_blank"><span style="font-size: 18px">Phương thức nặc danh</span></a><span style="font-size: 18px"> (Anonymous Method) trong C# cung cấp một kỹ thuật để truyền một khối code như là một tham số delegate. Các phương thức nặc danh là các phương thức không có tên, chỉ có thân phương thức.</span></p><p></p><p><span style="font-size: 18px">[ATTACH=full]3133[/ATTACH]</span></p><p></p><p><span style="font-size: 18px">Bạn không cần xác định kiểu trả về trong một phương thức nặc danh; nó được suy ra từ lệnh return bên trong thân phương thức nặc danh đó.</span></p><p></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>Viết một Phương thức nặc danh trong C#</strong></span></span></p><p></p><p><span style="font-size: 18px">Các phương thức nặc danh (Anonymous Method) trong C# được khai báo với việc tạo instance của Delegate đó, với một từ khóa delegate. Ví dụ:</span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">delegate void NumberChanger(int n);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">...</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">NumberChanger nc = delegate(int x)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Phuong thuc nac danh: {0}", x);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">};</span></em></span></p><p><span style="font-size: 18px">Khối <em>Console.WriteLine("Anonymous Method: {0}", x);</em> là phần thân của phương thức nặc danh.</span></p><p></p><p><span style="font-size: 18px"><a href="https://vnkienthuc.com/threads/tim-hieu-ve-generic-trong-c.77387/" target="_blank">Delegate</a> có thể được gọi cả với các phương thức nặc danh cũng như các phương thức được đặt tên theo cùng cách, ví dụ: bằng việc truyền các tham số phương thức tới đối tượng Delegate đó.</span></p><p><span style="font-size: 18px">Ví dụ: nc(10);</span></p><p></p><p><span style="font-size: 18px"><strong>Ví dụ minh họa ví dụ trên:</strong></span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">delegate void NumberChanger(int n);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> class TestCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static int num = 10;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public static void AddNum(int p)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> num += p;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Phuong thuc co ten: {0}", num);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public static void MultNum(int q)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> num *= q;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Phuong thuc co ten: {0}", num);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> public static int getNum()</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> return num;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Phuong thuc nac danh trong C#");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("--------------------------------");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> //tao doi tuong delegate su dung phuong thuc nac danh</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> NumberChanger nc = delegate(int x)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Phuong thuc nac danh: {0}", x);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> };</span></em></span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> //goi delegate su dung phuong thuc nac danh</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> nc(10);</span></em></span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> //khoi tao delegate su dung phuong thuc co ten</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> nc = new NumberChanger(AddNum);</span></em></span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> //khoi tao delegate su dung phuong thuc co ten</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> nc(5);</span></em></span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> //khoi tao delegate su dung phuong thuc co ten khac</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> nc = new NumberChanger(MultNum);</span></em></span></p><p></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> //goi delegate su dung phuong thuc co ten</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> nc(2);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.ReadKey();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px">Nếu bạn không sử dụng<a href="https://vnkienthuc.com/threads/tim-hieu-ve-lop-bitarray-trong-c.77384/" target="_blank"> lệnh Console.ReadKey();</a> thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.</span></p><p></p><p><span style="font-size: 18px">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p></p><p style="text-align: center"><span style="font-size: 18px"><img src="https://vietjack.com/csharp/images/phuong-thuc-nac-danh.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p> <p style="text-align: center"></p> <p style="text-align: right"><span style="font-size: 18px">Nguồn: vietjack.com</span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177638, member: 165510"] [URL='https://vnkienthuc.com/forums/code.377/'][SIZE=5]Phương thức nặc danh[/SIZE][/URL][SIZE=5] (Anonymous Method) trong C# cung cấp một kỹ thuật để truyền một khối code như là một tham số delegate. Các phương thức nặc danh là các phương thức không có tên, chỉ có thân phương thức.[/SIZE] [SIZE=5][ATTACH=full]3133._xfImport[/ATTACH][/SIZE] [SIZE=5]Bạn không cần xác định kiểu trả về trong một phương thức nặc danh; nó được suy ra từ lệnh return bên trong thân phương thức nặc danh đó.[/SIZE] [SIZE=5][COLOR=#0000ff][B]Viết một Phương thức nặc danh trong C#[/B][/COLOR][/SIZE] [SIZE=5]Các phương thức nặc danh (Anonymous Method) trong C# được khai báo với việc tạo instance của Delegate đó, với một từ khóa delegate. Ví dụ:[/SIZE] [SIZE=5][I][COLOR=#5900b3]delegate void NumberChanger(int n);[/COLOR][/I] [I][COLOR=#5900b3]...[/COLOR][/I] [I][COLOR=#5900b3]NumberChanger nc = delegate(int x)[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Phuong thuc nac danh: {0}", x);[/COLOR][/I] [I][COLOR=#5900b3]};[/COLOR][/I] Khối [I]Console.WriteLine("Anonymous Method: {0}", x);[/I] là phần thân của phương thức nặc danh.[/SIZE] [SIZE=5][URL='https://vnkienthuc.com/threads/tim-hieu-ve-generic-trong-c.77387/']Delegate[/URL] có thể được gọi cả với các phương thức nặc danh cũng như các phương thức được đặt tên theo cùng cách, ví dụ: bằng việc truyền các tham số phương thức tới đối tượng Delegate đó. Ví dụ: nc(10);[/SIZE] [SIZE=5][B]Ví dụ minh họa ví dụ trên:[/B][/SIZE] [SIZE=5][I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]delegate void NumberChanger(int n);[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestCsharp[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] static int num = 10;[/COLOR][/I] [I][COLOR=#5900b3] public static void AddNum(int p)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] num += p;[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Phuong thuc co ten: {0}", num);[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I][/SIZE] [SIZE=5][I][COLOR=#5900b3] public static void MultNum(int q)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] num *= q;[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Phuong thuc co ten: {0}", num);[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I][/SIZE] [SIZE=5][I][COLOR=#5900b3] public static int getNum()[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] return num;[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] static void Main(string[] args)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Phuong thuc nac danh trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("--------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] //tao doi tuong delegate su dung phuong thuc nac danh[/COLOR][/I] [I][COLOR=#5900b3] NumberChanger nc = delegate(int x)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Phuong thuc nac danh: {0}", x);[/COLOR][/I] [I][COLOR=#5900b3] };[/COLOR][/I][/SIZE] [SIZE=5][I][COLOR=#5900b3] //goi delegate su dung phuong thuc nac danh[/COLOR][/I] [I][COLOR=#5900b3] nc(10);[/COLOR][/I][/SIZE] [SIZE=5][I][COLOR=#5900b3] //khoi tao delegate su dung phuong thuc co ten[/COLOR][/I] [I][COLOR=#5900b3] nc = new NumberChanger(AddNum);[/COLOR][/I][/SIZE] [SIZE=5][I][COLOR=#5900b3] //khoi tao delegate su dung phuong thuc co ten[/COLOR][/I] [I][COLOR=#5900b3] nc(5);[/COLOR][/I][/SIZE] [SIZE=5][I][COLOR=#5900b3] //khoi tao delegate su dung phuong thuc co ten khac[/COLOR][/I] [I][COLOR=#5900b3] nc = new NumberChanger(MultNum);[/COLOR][/I][/SIZE] [SIZE=5][I][COLOR=#5900b3] //goi delegate su dung phuong thuc co ten[/COLOR][/I] [I][COLOR=#5900b3] nc(2);[/COLOR][/I] [I][COLOR=#5900b3] Console.ReadKey();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] Nếu bạn không sử dụng[URL='https://vnkienthuc.com/threads/tim-hieu-ve-lop-bitarray-trong-c.77384/'] lệnh Console.ReadKey();[/URL] thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.[/SIZE] [SIZE=5]Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:[/SIZE] [CENTER][SIZE=5][IMG]https://vietjack.com/csharp/images/phuong-thuc-nac-danh.PNG[/IMG][/SIZE] [/CENTER] [RIGHT][SIZE=5]Nguồn: vietjack.com[/SIZE][/RIGHT] [/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