C# extern修飾符概述
作者:佚名
這里介紹C# extern修飾符和DllImport一起使用時要加上 static 修飾符也可以用于對于同一程序集不同版本組件的調(diào)用,不能與 abstract 修飾符同時使用。
C#語言還是比較常見的東西,這里我們主要介紹C# extern修飾符,包括介紹成員函數(shù)經(jīng)常用于系統(tǒng)API函數(shù)的調(diào)用等方面。
C# extern修飾符是什么意思?
C# extern修飾符用于聲明 由程序集外部實現(xiàn)的成員函數(shù)經(jīng)常用于系統(tǒng)API函數(shù)的調(diào)用(通過 DllImport )。注意,C# extern修飾符和DllImport一起使用時要加上 static 修飾符也可以用于對于同一程序集不同版本組件的調(diào)用(用 extern 聲明別名) 不能與 abstract 修飾符同時使用 。
示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace Example03
- {
- class Program
- {
- //注意DllImport是一個Attribute Property,
- //在System.Runtime.InteropServices命名空間中定義
- //extern與DllImport一起使用時必須再加上一個static修飾符
- [DllImport("User32.dll")]
- public static extern int
- MessageBox(int Handle, string Message, string Caption, int
- Type);
- static int Main()
- {
- string myString;
- Console.Write("Enter your message: ");
- myString = Console.ReadLine();
- return MessageBox(0, myString, "My Message Box", 0);
- }
- }
- }
【編輯推薦】
責任編輯:佚名
來源:
博客園