日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
簡介C#Extern修飾符的作用

c# extern修飾符用于聲明在外部實現(xiàn)的方法。

c# extern 修飾符的常見用法是在使用 Interop 服務(wù)調(diào)入非托管代碼時與 DllImport 屬性一起使用;在這種情況下,該方法還必須聲明為 static,如下面的示例所示:

 
 
 
  1. [DllImport("avifil32.dll")]
  2. private static extern void AVIFileInit();

注意

extern 關(guān)鍵字還可以定義外部程序集別名,使得可以從單個程序集中引用同一組件的不同版本。

將 abstract 和 extern 修飾符一起使用來修改同一成員是錯誤的。

使用

c# extern 修飾符意味著方法在 C# 代碼的外部實現(xiàn),而使用 abstract 修飾符意味著在類中未提供方法實現(xiàn)。注意 extern 關(guān)鍵字在使用上比在 C++ 中有更多的限制。

示例

在該示例中,程序接收來自用戶的字符串并將該字符串顯示在消息框中。程序使用從 User32.dll庫導(dǎo)入的 MessageBox 方法。

 
 
 
  1. using System; using System.Runtime.InteropServices; 
  2. class MainClass 
  3. {
  4. [DllImport("User32.dll")]
  5. public static extern int MessageBox(int h, string m, string c, int type);
  6. static int Main() 
  7. string myString;
  8. Console.Write("Enter your message: ");
  9. myString = Console.ReadLine(); 
  10. return MessageBox(0, myString, "My Message Box", 0); 
  11. }
  12. 此示例使用 C 程序創(chuàng)建一個 DLL,在下一示例中將從 C# 程序調(diào)用該 DLL。
  13. // cmdll.c // compile with: 
  14. /LD int __declspec(dllexport) SampleMethod(int i) 
  15. {
  16. return i*10;

該示例使用兩個文件 CM.cs 和 Cmdll.c 來說明 extern。

C 文件是示例 2 中創(chuàng)建的外部 DLL,它從C# 程序內(nèi)調(diào)用。

 
 
 
  1. // cm.cs using System; 
  2. using System.Runtime.InteropServices; 
  3. public class MainClass {
  4. [DllImport("Cmdll.dll")] 
  5. public static extern int SampleMethod(int x);
  6. static void Main()
  7. Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5)); 

輸出SampleMethod() returns 50. 備注生成項目: 使用 Visual C++ 命令行將 Cmdll.c 編譯為 DLL: cl /LD Cmdll.c 使用命令行編譯 CM.cs: csc CM.cs 這將創(chuàng)建可執(zhí)行文件 CM.exe。運行此程序時,SampleMethod 將值 5 傳遞到 DLL 文件,該文件將此值乘以 10 返回。

 好了,C# Extern修飾符的作用介紹到這里。


網(wǎng)頁標題:簡介C#Extern修飾符的作用
鏈接分享:http://www.5511xx.com/article/djpcjog.html