首页 > c# > extern 方法声明指外部使用方法

extern 方法声明指外部使用方法

在方法声明中使用 extern 修饰符指示在外部实现方法。外部修饰符的常见用法是与 DllImport 属性一起使用。自己编写的类,直接添加引用即可,不需要COM交互,不必使用这种方式。将 abstract 和 extern 修饰符一起使用来修改同一成员是错误的。使用 extern 修饰符意味着方法在 C# 代码的外部实现,而使用 abstract 修饰符意味着在此类中未提供此方法的实现。因为外部方法声明不提供实现,所以没有方法体;此方法声明只是以一个分号结束(在签名后没有大括号 {})。

extern 方法声明指外部使用方法,与DllImport属性一起使用,不能与abstract一起使用
定义如下:
public static extern int MyMethod(int x); 没有"{}"
例子运行结果输入文字后,弹出一个窗口显示输入的文字。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace testextern
{
class Program
{

[DllImport("User32.dll")]

public static extern int MessageBox(int h, string m, string c, int type);

public static int Main()
{

string myString;

Console.Write("Enter your message: ");

myString = Console.ReadLine();

return MessageBox(0, myString, "My Message Box", 0);

}

}
}

原创文章转载请注明出处:云飞扬IT的blog

本文链接: http://www.ajaxcn.net/archives/330

分类: c# 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.