extern 修飾符用于聲明由程序集外部實(shí)現(xiàn)的成員函數(shù)
經(jīng)常用于系統(tǒng)API函數(shù)的調(diào)用(通過 DllImport )料扰。注意智末,和DllImport一起使用時(shí)要加上 static 修飾符
也可以用于對(duì)于同一程序集不同版本組件的調(diào)用(用 extern 聲明別名)
不能與 abstract 修飾符同時(shí)使用
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Runtime.InteropServices;
namespaceExample03
{
classProgram
{
//注意DllImport是一個(gè)Attribute Property采转,在System.Runtime.InteropServices命名空間中定義
//extern與DllImport一起使用時(shí)必須再加上一個(gè)static修飾符
[DllImport("User32.dll")]
publicstaticexternintMessageBox(intHandle,stringMessage,stringCaption,intType);
staticintMain()
{
stringmyString;
Console.Write("Enter your message: ");
myString = Console.ReadLine();
returnMessageBox(0, myString,"My Message Box", 0);
}
}
}
結(jié)果: