簡單介紹C#預(yù)處理
C#預(yù)處理
C#預(yù)處理階段是一個(gè)文本到文本的轉(zhuǎn)換階段,在預(yù)處理過程中,使能進(jìn)行代碼的條件包含和排除。
- pp-un it:
- pp-gro up opt
- pp-gro up:
- pp-gro up-part
- pp-gro up pp-group-part
- pp-gro up-part:
- pp-tokensopt new-line
- pp-de claration
- pp-if -section
- pp-con trol-line
- pp-l ine-number
- pp-tokens:
- pp-token
- pp-tokens pp-token
- pp-token:
- identifi er
- keyword
- literal
- operator-or-punctuator
- new-line:
- The carriage return character (U+000D)
- The line feed character (U+000A)
- The carriage return character followed by a line feed character
- The line separator character (U+2028)
- The paragraph separator character (U+2029)
C#預(yù)處理聲明
在預(yù)處理過程中,為了使用名稱可以被定義和取消定義。#define 定義一個(gè)標(biāo)識符。#undef “反定義”一個(gè)標(biāo)識符,如果一個(gè)標(biāo)識符在以前已經(jīng)被定義了,那么它就變成了不明確的。如果一個(gè)標(biāo)識符已經(jīng)被定義了,它的語意就等同于true ;如果一個(gè)標(biāo)識符沒有意義,那么它的語意等同于false.
- pp-de claration:
- #define pp-identifier
- #undef pp-identifier
來看看這個(gè)例子:
- #define A
- #undef B
- class C
- {
- #if A
- void F()
- #else
- void G()
- #endif
- #if B
- void H()
- #else
- void I()
- #endif
- }
- 變?yōu)?
- class C
- {
- void F()
- void I()
- }
如果有一個(gè)pp-unit, 聲明就必須用pp- token 元素進(jìn)行。換句話說,#define 和#undef 必須在文件中任何 “真正代碼”前聲明,否則在編譯時(shí)會(huì)發(fā)生錯(cuò)誤。因此,也許會(huì)像下面的例子一樣散布#if 和#define:
- define A
- #if A
- #define B
- #endif
- namespace N
- {
- #if B
- class Class1
- #endif
- }
因?yàn)?define 放在了真實(shí)代碼后面,所以下面的例子是非法的:
- #define A
- namespace N
- {
- #define B
- #if B
- class Class1
- #endif
- }
以上介紹C#預(yù)處理
【編輯推薦】