extern

extern modifier is used to declare a method that is implemented externally." xml:space="preserve" style="line-height: 1.6; font-family: Simsun; font-size: medium;">extern 修饰符用于声明在外部实现的方法。 extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code." xml:space="preserve" style="line-height: 1.6; font-family: Simsun; font-size: medium;"> extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 特性一起使用。 static, as shown in the following example:" xml:space="preserve" style="line-height: 1.6; font-family: Simsun; font-size: medium;">在这种情况下,还必须将方法声明为 static,如下面的示例所示:


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

extern keyword can also define an external assembly alias, which makes it possible to reference different versions of the same component from within a single assembly." xml:space="preserve">extern 关键字还可以定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。外部别名(C# 参考)." xml:space="preserve">有关详细信息,请参阅 外部别名(C# 参考)

abstract and extern modifiers together to modify the same member." xml:space="preserve">将 abstract 和 extern 修饰符一起使用来修改同一成员是错误的做法。extern modifier means that the method is implemented outside the C# code, whereas using the abstract modifier means that the method implementation is not provided in the class." xml:space="preserve">使用 extern 修饰符意味着方法是在 C# 代码的外部实现的,而使用 abstract 修饰符意味着类中未提供方法实现。

extern 关键字用于 C# 中时会比用于 C++ 中时受到更多的限制。若要比较 C# 关键字与 C++ 关键字,请参见 C++ 语言参考中的“使用 extern 指定链接”。

Example 1. In this example, the program receives a string from the user and displays it inside a message box." xml:space="preserve">示例 1. 在此示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。MessageBox method imported from the User32.dll library." xml:space="preserve">程序使用从 User32.dll 库导入的 MessageBox 方法。

C#

//using System.Runtime.InteropServices; class ExternTest{ [DllImport( "User32.dll", CharSet=CharSet.Unicode)] public static extern int MessageBox(IntPtr h, string m, string c, int type); static int Main() { string myString; Console.Write( "Enter your message: "); myString = Console.ReadLine(); return MessageBox((IntPtr)0, myString, "My Message Box", 0); }}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值