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 giá trị 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: 177519" data-attributes="member: 165510"><p style="text-align: center"><strong><span style="color: #ff0000">Kỹ thuật 1: TRUYỀN THAM SỐ BỞI GIÁ TRỊ TRONG C#</span></strong></p> <p style="text-align: center"></p><p>Đây là kỹ thuật mặc định để truyền các tham số tới một phương thức. Trong kỹ thuật này, khi một phương thức được gọi, một vị trí kho lưu mới được tạo cho mỗi tham số giá trị đó.</p><p></p><p>Các giá trị của các tham số thực sự được sao chép vào trong chúng. Vì thế, các thay đổi được tạo ra với các tham số bên trong phương thức không có ảnh hưởng gì tới các tham số chính thức. Ví dụ sau minh họa khái niệm này.</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 swap(int x, int y)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> int temp; //bien tam</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> temp = x; /* luu giu gia tri x vao bien tam*/</span></em></p><p><em><span style="color: #5900b3"> x = y; /* dat gia tri cua y vao x */</span></em></p><p><em><span style="color: #5900b3"> y = temp; /* dat gia tri cua temp vao y */</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 gia tri trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Trao doi gia tri 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"> /* 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"> int b = 200;</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Truoc khi trao doi, gia tri cua a la: {0}", a);</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Truoc khi trao doi, gia tri cua b la: {0}", b);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> /* goi ham de trao doi gia tri */</span></em></p><p><em><span style="color: #5900b3"> n.swap(a, b);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("\nSau khi trao doi, gia tri cua a la: {0}", a);</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Sau khi trao doi, gia tri cua b la: {0}", b);</span></em></p><p><em><span style="color: #5900b3"></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-gia-tri.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p> <p style="text-align: center"></p><p>Điều này chỉ ra rằng không có sự thay đổi trong các giá trị bởi vì chúng đã được thay đổi bên trong hàm.</p><p></p><p>Đây là một trong 3 kỹ thuật truyền tham số tới một phương thức. Hai kỹ thuật còn lại các bạn xem tại link này:</p><p>Kỹ thuật truyền tham số bởi output: <a href="https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-output-trong-c.t77291/" target="_blank">https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-output-trong-c.t77291/</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></p><p style="text-align: right">Nguồn: vietjeck.com</p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177519, member: 165510"] [CENTER][B][COLOR=#ff0000]Kỹ thuật 1: TRUYỀN THAM SỐ BỞI GIÁ TRỊ TRONG C#[/COLOR][/B] [/CENTER] Đây là kỹ thuật mặc định để truyền các tham số tới một phương thức. Trong kỹ thuật này, khi một phương thức được gọi, một vị trí kho lưu mới được tạo cho mỗi tham số giá trị đó. Các giá trị của các tham số thực sự được sao chép vào trong chúng. Vì thế, các thay đổi được tạo ra với các tham số bên trong phương thức không có ảnh hưởng gì tới các tham số chính thức. Ví dụ sau minh họa khái niệm này. [I][COLOR=#5900b3]using System; namespace VietJackCsharp { class TestCsharp { public void swap(int x, int y) { int temp; //bien tam temp = x; /* luu giu gia tri x vao bien tam*/ x = y; /* dat gia tri cua y vao x */ y = temp; /* dat gia tri cua temp vao y */ } static void Main(string[] args) { Console.WriteLine("Truyen tham so boi gia tri trong C#"); Console.WriteLine("Trao doi gia tri trong C#"); Console.WriteLine("------------------------------------"); //tao doi tuong TestCsharp TestCsharp n = new TestCsharp(); /* phan dinh nghia bien cuc bo */ int a = 100; int b = 200; Console.WriteLine("Truoc khi trao doi, gia tri cua a la: {0}", a); Console.WriteLine("Truoc khi trao doi, gia tri cua b la: {0}", b); /* goi ham de trao doi gia tri */ n.swap(a, b); Console.WriteLine("\nSau khi trao doi, gia tri cua a la: {0}", a); Console.WriteLine("Sau khi trao doi, 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-gia-tri.PNG[/IMG] [/CENTER] Điều này chỉ ra rằng không có sự thay đổi trong các giá trị bởi vì chúng đã được thay đổi bên trong hàm. Đây là một trong 3 kỹ thuật truyền tham số tới một phương thức. Hai kỹ thuật còn lại các bạn xem tại link này: Kỹ thuật truyền tham số bởi output: [URL]https://vnkienthuc.com/huong-dan-truyen-tham-so-boi-output-trong-c.t77291/[/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 giá trị trong C#
Top