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
Hướng dẫn truyền tham số bởi Output 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: 177521" data-attributes="member: 165510"><p style="text-align: center"><span style="color: #ff0000"><strong>Kỹ thuật 3: TRUYỀN THAM SỐ BỞI OUTPUT TRONG C#</strong></span></p><p></p><p>Một lệnh return có thể được sử dụng để chỉ trả về một giá trị từ một hàm. Tuy nhiên, sử dụng các tham số output, bạn có thể trả về hai giá trị từ một hàm. Các tham số output là tương tự như các tham số tham chiếu, ngoại trừ chúng truyền dữ liệu ra khỏi phương thức thay vì vào trong nó.</p><p></p><p>Dưới đây là ví dụ minh họa tham số output trong C#:</p><p></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> public void getValue(out int x)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> int temp = 5;</span></em></p><p><em><span style="color: #5900b3"> x = temp;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Truyen tham so boi output trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("-------------------------------------");</span></em></p><p><em><span style="color: #5900b3"> //tao doi tuong TestCsharp</span></em></p><p><em><span style="color: #5900b3"> TestCsharp n = new TestCsharp();</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> /* phan dinh nghia bien cuc bo */</span></em></p><p><em><span style="color: #5900b3"> int a = 100;</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Truoc khi goi phuong thuc, gia tri cua a la: {0}", a);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> /* goi ham getValue() de lay gia tri */</span></em></p><p><em><span style="color: #5900b3"> n.getValue(out a);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Sau khi goi phuong thuc, gia tri cua a la: {0}", a);</span></em></p><p><em><span style="color: #5900b3"> Console.ReadLine();</span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p>Nếu bạn không sử dụng lệnh Console.ReadKey(); 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.</p><p></p><p>Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</p><p></p><p style="text-align: center"><img src="https://vietjack.com/csharp/images/truyen-tham-so-boi-output.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p>Biến được cung cấp cho tham số output không cần được gán một giá trị. Các tham số output là đặc biệt hữu ích khi bạn cần trả về các giá trị từ một phương thức thông qua các tham số đó mà không cần gán một giá trị khởi đầu cho tham số đó. Bạn xét ví dụ minh họa sau để hiểu khái niệm này trong C#:</p><p></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> public void getValues(out int x, out int y)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.Write("Nhap gia tri thu nhat: ");</span></em></p><p><em><span style="color: #5900b3"> x = Convert.ToInt32(Console.ReadLine());</span></em></p><p><em><span style="color: #5900b3"> Console.Write("Nhap gia tri thu hai: ");</span></em></p><p><em><span style="color: #5900b3"> y = Convert.ToInt32(Console.ReadLine());</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Truyen tham so boi output trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("-------------------------------------\n");</span></em></p><p><em><span style="color: #5900b3"> //tao doi tuong TestCsharp</span></em></p><p><em><span style="color: #5900b3"> TestCsharp n = new TestCsharp();</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> /* phan dinh nghia bien cuc bo */</span></em></p><p><em><span style="color: #5900b3"> int a, b;</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> /* goi ham getValues() de lay gia tri */</span></em></p><p><em><span style="color: #5900b3"> n.getValues(out a, out b);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Sau loi goi phuong thuc, gia tri cua a la: {0}", a);</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Sau loi goi phuong thuc, gia tri cua b la: {0}", b);</span></em></p><p><em><span style="color: #5900b3"> Console.ReadLine();</span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p>Nếu bạn không sử dụng lệnh Console.ReadKey(); 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.</p><p></p><p>Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</p><p></p><p style="text-align: center"><img src="https://vietjack.com/csharp/images/truyen-tham-so-boi-output-1.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p> <p style="text-align: center"></p><p>Trên đây là bài viết giới thiệu về ký thuật truyền tham số bởi Output, là một trong ba kỹ thuật truyền tham số tới phương thức trong C#. Các bạn có thể xem thêm 2 kỹ thuật còn lại tại link này:</p><p>Kỹ thuật truyền tham số bởi giá trị: <a href="https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-gia-tri-trong-c.t77289/" target="_blank">https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-gia-tri-trong-c.t77289/</a></p><p>Ký thuật truyền tham số bởi tham chiếu: <a href="https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-tham-chieu-trong-c.t77290/" target="_blank">https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-tham-chieu-trong-c.t77290/</a></p><p style="text-align: right">Nguồn: vietjeck.com</p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177521, member: 165510"] [CENTER][COLOR=#ff0000][B]Kỹ thuật 3: TRUYỀN THAM SỐ BỞI OUTPUT TRONG C#[/B][/COLOR][/CENTER] Một lệnh return có thể được sử dụng để chỉ trả về một giá trị từ một hàm. Tuy nhiên, sử dụng các tham số output, bạn có thể trả về hai giá trị từ một hàm. Các tham số output là tương tự như các tham số tham chiếu, ngoại trừ chúng truyền dữ liệu ra khỏi phương thức thay vì vào trong nó. Dưới đây là ví dụ minh họa tham số output trong C#: [I][COLOR=#5900b3]using System; namespace VietJackCsharp { class TestCsharp { public void getValue(out int x) { int temp = 5; x = temp; } static void Main(string[] args) { Console.WriteLine("Truyen tham so boi output trong C#"); Console.WriteLine("-------------------------------------"); //tao doi tuong TestCsharp TestCsharp n = new TestCsharp(); /* phan dinh nghia bien cuc bo */ int a = 100; Console.WriteLine("Truoc khi goi phuong thuc, gia tri cua a la: {0}", a); /* goi ham getValue() de lay gia tri */ n.getValue(out a); Console.WriteLine("Sau khi goi phuong thuc, gia tri cua a la: {0}", a); Console.ReadLine(); Console.ReadKey(); } } }[/COLOR][/I] Nếu bạn không sử dụng lệnh Console.ReadKey(); 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. Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [CENTER][IMG]https://vietjack.com/csharp/images/truyen-tham-so-boi-output.PNG[/IMG][/CENTER] Biến được cung cấp cho tham số output không cần được gán một giá trị. Các tham số output là đặc biệt hữu ích khi bạn cần trả về các giá trị từ một phương thức thông qua các tham số đó mà không cần gán một giá trị khởi đầu cho tham số đó. Bạn xét ví dụ minh họa sau để hiểu khái niệm này trong C#: [I][COLOR=#5900b3]using System; namespace VietJackCsharp { class TestCsharp { public void getValues(out int x, out int y) { Console.Write("Nhap gia tri thu nhat: "); x = Convert.ToInt32(Console.ReadLine()); Console.Write("Nhap gia tri thu hai: "); y = Convert.ToInt32(Console.ReadLine()); } static void Main(string[] args) { Console.WriteLine("Truyen tham so boi output trong C#"); Console.WriteLine("-------------------------------------\n"); //tao doi tuong TestCsharp TestCsharp n = new TestCsharp(); /* phan dinh nghia bien cuc bo */ int a, b; /* goi ham getValues() de lay gia tri */ n.getValues(out a, out b); Console.WriteLine("Sau loi goi phuong thuc, gia tri cua a la: {0}", a); Console.WriteLine("Sau loi goi phuong thuc, gia tri cua b la: {0}", b); Console.ReadLine(); Console.ReadKey(); } } }[/COLOR][/I] Nếu bạn không sử dụng lệnh Console.ReadKey(); 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. Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [CENTER][IMG]https://vietjack.com/csharp/images/truyen-tham-so-boi-output-1.PNG[/IMG] [/CENTER] Trên đây là bài viết giới thiệu về ký thuật truyền tham số bởi Output, là một trong ba kỹ thuật truyền tham số tới phương thức trong C#. Các bạn có thể xem thêm 2 kỹ thuật còn lại tại link này: Kỹ thuật truyền tham số bởi giá trị: [URL]https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-gia-tri-trong-c.t77289/[/URL] Ký thuật truyền tham số bởi tham chiếu: [URL]https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-tham-chieu-trong-c.t77290/[/URL] [RIGHT]Nguồn: vietjeck.com[/RIGHT] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Hướng dẫn truyền tham số bởi Output trong C#
Top