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ề Chỉ thị tiền xử lý 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: 177571" data-attributes="member: 165510"><p><span style="font-size: 18px">C# có 1 số</span><a href="https://vnkienthuc.com/forums/code.377/" target="_blank"><span style="font-size: 18px"> lệnh tiền xử lí</span></a><span style="font-size: 18px"> (Preprocessor Directive), những lệnh này không bao giờ được biên dịch thành bất kì dòng lệnh nào trong mã thực thi. Thay vào đó nó có ảnh hưởng đến các khía cạnh của quy trình biên dịch. Ví dụ , ta có thể dùng chỉ dẫn tiền xử lí để ngăn trình biên dịch biên dịch một phần đoạn mã nào đó .Chỉ thị tiền xử lí được phân biệt bằng cách bắt đầu với dấu # .</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Như vậy, các chỉ thị tiền xử lý cung cấp chỉ lệnh tới </span><a href="https://vnkienthuc.com/threads/tim-hieu-ve-attribute-trong-c.77351/" target="_blank"><span style="font-size: 18px">compiler</span></a><span style="font-size: 18px"> để tiền xử lý thông tin trước khi sự biên dịch thực sự bắt đầu.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Chỉ có các ký tự white-space có thể xuất hiện ở trước một chỉ thị tiền xử lý trong một dòng. Các chỉ thị tiền xử lý trong C# không là các lệnh, vì thế chúng không kết thúc với một dấu chấm phảy </span></p><p><span style="font-size: 18px"></span></p><p><a href="https://vnkienthuc.com/threads/tim-hieu-ve-reflection-trong-c.77353/" target="_blank"><span style="font-size: 18px">Bộ biên dịch của C# </span></a><span style="font-size: 18px">không có một bộ tiền xử lý riêng biệt, tuy nhiên, các chỉ thị này được xử lý như khi thực sự có một bộ tiền xử lý riêng vậy. Trong C#, các chỉ thị tiền xử lý được sử dụng để giúp ích việc biên dịch có điều kiện. Không giống các chỉ thị tiền xử lý trong C và C++, chúng không được sử dụng để tạo các macro. Một chỉ thị tiền xử lý phải chỉ là một chỉ lệnh trên một dòng.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>1. Các chỉ thị tiền xử lý trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Danh sách các chỉ thị tiền xử lý có sẵn trong C#:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#define: </span>Nó định nghĩa một dãy ký tự, được gọi là các biểu tượng</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#undef: </span>Nó cho phép bạn không định nghĩa (undefine) một biểu tượng</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#if:</span> Nó cho phép kiểm tra một biểu tượng hoặc nhiều biểu tượng để thấy nếu chúng ước lượng là true</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#else</span>: Nó cho phép tạo một chỉ thị có điều kiện phức hợp, cùng với #if</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#elif:</span> Nó cho phép tạo một chỉ thị có điều kiện phức hợp</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#endif:</span> Xác định phần cuối của một chỉ thị có điều kiện (conditional directive)</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#line:</span> Nó cho phép bạn sửa đổi số dòng của compiler và (tùy ý) tên file cho Error và Warning</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#error:</span> Nó cho phép tạo một error từ một vị trí cụ thể trong code của bạn</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#warning:</span> Nó cho phép tạo một mức độ cảnh báo từ một vị trí cụ thể trong code của bạn</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#region:</span> Nó cho phép bạn xác định một khối code mà bạn có thể mở rộng hoặc thu gọn bởi sử dụng đặc điểm của Visual Studio Code Editor</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">#endregion:</span> Nó đánh dấu phần cuối của một khối #region</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #00b300"><strong>1.1. Chỉ thị tiền xử lý #define trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Chỉ thị </span><a href="https://vnkienthuc.com/forums/cong-nghe-thong-tin.375/" target="_blank"><span style="font-size: 18px">tiền xử lý #define trong C# </span></a><span style="font-size: 18px">tạo các hằng biểu tượng.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">#define cho phép bạn tạo một biểu tượng như vậy, bởi sử dụng biểu tượng dạng biểu thức được truyền tới chỉ thị tiền xử lý #if, biểu thức ước lượng là true. Cú pháp của nó như sau:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">#define symbol</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Ví dụ sau minh họa điều này:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">#define PI</span></em></span></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">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 TestClass</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("Chi thi tien xu ly trong C#");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa chi thi tien xu ly #define");</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"> #if (PI)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("PI da duoc dinh nghia");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> #else</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("PI chua duoc dinh nghia");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> #endif</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 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.</span></p><p><span style="font-size: 18px"></span></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><span style="font-size: 18px"></span></p><p style="text-align: center"><span style="font-size: 18px"><img src="https://vietjack.com/csharp/images/chi-thi-tien-xu-ly.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #00b300"><strong>1.2. Chỉ thị có điều kiện (Conditional Directive) trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Bạn có thể sử dụng chỉ thị tiền xử lý #if trong C# để tạo một chỉ thị có điều kiện </span><a href="https://vnkienthuc.com/forums/hoi-dap-tin-hoc.378/" target="_blank"><span style="font-size: 18px">(Conditional Directive).</span></a><span style="font-size: 18px"> Các chỉ thị có điều kiện là hữu ích khi kiểm tra một biểu tượng hoặc các biểu tượng để kiểm tra nếu chúng ước lượng là true. Nếu chúng ước lượng là true, compiler ước lượng tất cả code giữa chỉ thị #if và chỉ thị tiếp theo.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Cú pháp cho chỉ thị có điều kiện trong C# là:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">#if symbol [operator symbol]...</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Tại đây, <em>symbol</em> là tên của biểu tượng bạn muốn kiểm tra. Bạn cũng có thể sử dụng true và false hoặc phụ thêm vào sau biểu tượng với toán tử phủ định.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em>operator symbol</em> là toán tử được sử dụng để ước lượng biểu tượng đó. Các toán tử có thể là một trong các:</span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px">== (bằng)</span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">!= (không bằng)</span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">&& (và)</span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">|| (hoặc)</span></li> </ul><p><span style="font-size: 18px">Bạn cũng có thể nhóm các biểu tượng và toán tử bởi các dấu ngoặc đơn. Các chỉ thị có điều kiện được sử dụng để biên dịch code cho debug hoặc khi biên dịch cho một sự định cấu hình cụ thể. Một chỉ thị có điều kiện trong C# bắt đầu với một chỉ thị tiền xử lý #if phải được kết thúc một cách rõ ràng bởi một chỉ thị #endif.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Ví dụ sau minh họa sự sử dụng các chỉ thị có điều kiện trong C#:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">#define DEBUG</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">#define VC_V10</span></em></span></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">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 TestClass</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("Chi thi tien xu ly trong C#");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa chi thi tien xu ly #define");</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"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> #if (DEBUG && !VC_V10)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("DEBUG da duoc dinh nghia");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> #elif (!DEBUG && VC_V10)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("VC_V10 da duoc dinh nghia");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> #elif (DEBUG && VC_V10)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("DEBUG va VC_V10 da duoc dinh nghia");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> #else</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("DEBUG va VC_V10 chua duoc dinh nghia");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> #endif</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">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p><span style="font-size: 18px"></span></p><p style="text-align: center"><span style="font-size: 18px"><img src="https://vietjack.com/csharp/images/chi-thi-tien-xu-ly-1.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Chúc các bạn ngày làm việc vui vẻ, hiệu quả!</span></p><p><span style="font-size: 18px"></span></p><p style="text-align: right"><span style="font-size: 18px">Nguồn: vietjack.com</span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177571, member: 165510"] [SIZE=5]C# có 1 số[/SIZE][URL='https://vnkienthuc.com/forums/code.377/'][SIZE=5] lệnh tiền xử lí[/SIZE][/URL][SIZE=5] (Preprocessor Directive), những lệnh này không bao giờ được biên dịch thành bất kì dòng lệnh nào trong mã thực thi. Thay vào đó nó có ảnh hưởng đến các khía cạnh của quy trình biên dịch. Ví dụ , ta có thể dùng chỉ dẫn tiền xử lí để ngăn trình biên dịch biên dịch một phần đoạn mã nào đó .Chỉ thị tiền xử lí được phân biệt bằng cách bắt đầu với dấu # . Như vậy, các chỉ thị tiền xử lý cung cấp chỉ lệnh tới [/SIZE][URL='https://vnkienthuc.com/threads/tim-hieu-ve-attribute-trong-c.77351/'][SIZE=5]compiler[/SIZE][/URL][SIZE=5] để tiền xử lý thông tin trước khi sự biên dịch thực sự bắt đầu. Chỉ có các ký tự white-space có thể xuất hiện ở trước một chỉ thị tiền xử lý trong một dòng. Các chỉ thị tiền xử lý trong C# không là các lệnh, vì thế chúng không kết thúc với một dấu chấm phảy [/SIZE] [URL='https://vnkienthuc.com/threads/tim-hieu-ve-reflection-trong-c.77353/'][SIZE=5]Bộ biên dịch của C# [/SIZE][/URL][SIZE=5]không có một bộ tiền xử lý riêng biệt, tuy nhiên, các chỉ thị này được xử lý như khi thực sự có một bộ tiền xử lý riêng vậy. Trong C#, các chỉ thị tiền xử lý được sử dụng để giúp ích việc biên dịch có điều kiện. Không giống các chỉ thị tiền xử lý trong C và C++, chúng không được sử dụng để tạo các macro. Một chỉ thị tiền xử lý phải chỉ là một chỉ lệnh trên một dòng. [COLOR=#0000ff][B]1. Các chỉ thị tiền xử lý trong C#[/B][/COLOR] Danh sách các chỉ thị tiền xử lý có sẵn trong C#: [COLOR=#ff8000]#define: [/COLOR]Nó định nghĩa một dãy ký tự, được gọi là các biểu tượng [COLOR=#ff8000]#undef: [/COLOR]Nó cho phép bạn không định nghĩa (undefine) một biểu tượng [COLOR=#ff8000]#if:[/COLOR] Nó cho phép kiểm tra một biểu tượng hoặc nhiều biểu tượng để thấy nếu chúng ước lượng là true [COLOR=#ff8000]#else[/COLOR]: Nó cho phép tạo một chỉ thị có điều kiện phức hợp, cùng với #if [COLOR=#ff8000]#elif:[/COLOR] Nó cho phép tạo một chỉ thị có điều kiện phức hợp [COLOR=#ff8000]#endif:[/COLOR] Xác định phần cuối của một chỉ thị có điều kiện (conditional directive) [COLOR=#ff8000]#line:[/COLOR] Nó cho phép bạn sửa đổi số dòng của compiler và (tùy ý) tên file cho Error và Warning [COLOR=#ff8000]#error:[/COLOR] Nó cho phép tạo một error từ một vị trí cụ thể trong code của bạn [COLOR=#ff8000]#warning:[/COLOR] Nó cho phép tạo một mức độ cảnh báo từ một vị trí cụ thể trong code của bạn [COLOR=#ff8000]#region:[/COLOR] Nó cho phép bạn xác định một khối code mà bạn có thể mở rộng hoặc thu gọn bởi sử dụng đặc điểm của Visual Studio Code Editor [COLOR=#ff8000]#endregion:[/COLOR] Nó đánh dấu phần cuối của một khối #region [COLOR=#00b300][B]1.1. Chỉ thị tiền xử lý #define trong C#[/B][/COLOR] Chỉ thị [/SIZE][URL='https://vnkienthuc.com/forums/cong-nghe-thong-tin.375/'][SIZE=5]tiền xử lý #define trong C# [/SIZE][/URL][SIZE=5]tạo các hằng biểu tượng. #define cho phép bạn tạo một biểu tượng như vậy, bởi sử dụng biểu tượng dạng biểu thức được truyền tới chỉ thị tiền xử lý #if, biểu thức ước lượng là true. Cú pháp của nó như sau: [I][COLOR=#5900b3]#define symbol[/COLOR][/I] Ví dụ sau minh họa điều này: [I][COLOR=#5900b3]#define PI[/COLOR][/I] [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestClass[/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("Chi thi tien xu ly trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Vi du minh hoa chi thi tien xu ly #define");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("------------------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] #if (PI)[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("PI da duoc dinh nghia");[/COLOR][/I] [I][COLOR=#5900b3] #else[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("PI chua duoc dinh nghia");[/COLOR][/I] [I][COLOR=#5900b3] #endif[/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 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: [/SIZE] [CENTER][SIZE=5][IMG]https://vietjack.com/csharp/images/chi-thi-tien-xu-ly.PNG[/IMG][/SIZE][/CENTER] [SIZE=5] [COLOR=#00b300][B]1.2. Chỉ thị có điều kiện (Conditional Directive) trong C#[/B][/COLOR] Bạn có thể sử dụng chỉ thị tiền xử lý #if trong C# để tạo một chỉ thị có điều kiện [/SIZE][URL='https://vnkienthuc.com/forums/hoi-dap-tin-hoc.378/'][SIZE=5](Conditional Directive).[/SIZE][/URL][SIZE=5] Các chỉ thị có điều kiện là hữu ích khi kiểm tra một biểu tượng hoặc các biểu tượng để kiểm tra nếu chúng ước lượng là true. Nếu chúng ước lượng là true, compiler ước lượng tất cả code giữa chỉ thị #if và chỉ thị tiếp theo. Cú pháp cho chỉ thị có điều kiện trong C# là: [I][COLOR=#5900b3]#if symbol [operator symbol]...[/COLOR][/I] Tại đây, [I]symbol[/I] là tên của biểu tượng bạn muốn kiểm tra. Bạn cũng có thể sử dụng true và false hoặc phụ thêm vào sau biểu tượng với toán tử phủ định. [I]operator symbol[/I] là toán tử được sử dụng để ước lượng biểu tượng đó. Các toán tử có thể là một trong các:[/SIZE] [LIST] [*][SIZE=5]== (bằng)[/SIZE] [*][SIZE=5]!= (không bằng)[/SIZE] [*][SIZE=5]&& (và)[/SIZE] [*][SIZE=5]|| (hoặc)[/SIZE] [/LIST] [SIZE=5]Bạn cũng có thể nhóm các biểu tượng và toán tử bởi các dấu ngoặc đơn. Các chỉ thị có điều kiện được sử dụng để biên dịch code cho debug hoặc khi biên dịch cho một sự định cấu hình cụ thể. Một chỉ thị có điều kiện trong C# bắt đầu với một chỉ thị tiền xử lý #if phải được kết thúc một cách rõ ràng bởi một chỉ thị #endif. Ví dụ sau minh họa sự sử dụng các chỉ thị có điều kiện trong C#: [I][COLOR=#5900b3]#define DEBUG[/COLOR][/I] [I][COLOR=#5900b3]#define VC_V10[/COLOR][/I] [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestClass[/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("Chi thi tien xu ly trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Vi du minh hoa chi thi tien xu ly #define");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("------------------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] #if (DEBUG && !VC_V10)[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("DEBUG da duoc dinh nghia");[/COLOR][/I] [I][COLOR=#5900b3] #elif (!DEBUG && VC_V10)[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("VC_V10 da duoc dinh nghia");[/COLOR][/I] [I][COLOR=#5900b3] #elif (DEBUG && VC_V10)[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("DEBUG va VC_V10 da duoc dinh nghia");[/COLOR][/I] [I][COLOR=#5900b3] #else[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("DEBUG va VC_V10 chua duoc dinh nghia");[/COLOR][/I] [I][COLOR=#5900b3] #endif[/COLOR][/I] [I][COLOR=#5900b3] Console.ReadKey();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] 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/chi-thi-tien-xu-ly-1.PNG[/IMG] [/SIZE][/CENTER] [SIZE=5]Chúc các bạn ngày làm việc vui vẻ, hiệu quả! [/SIZE] [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ề Chỉ thị tiền xử lý trong C#?
Top