C# Keywords Tutorial Part 30: extern
Amr Saafan
Founder | CTO | Software Architect & Consultant | Engineering Manager | Project Manager | Product Owner | +27K Followers | Now Hiring!
C# is a widely-used programming language with strong capabilities that allow for the creation of diverse applications. One of C#’s intriguing features is the “extern” keyword. This keyword serves to indicate that a field or method is defined externally to the current program. In this blog post, we will delve into the “extern” keyword in C# and its practical uses, with code examples.
The “extern” keyword in C# is used to specify that a method or field is implemented outside of the current program, typically in a separate native code library or a DLL (Dynamic Link Library). When we use the “extern” keyword, we are telling the C# compiler that we will provide the implementation of the method or field at runtime, and it should not try to compile it into the current program.
Let’s take a look at a simple example to understand how the “extern” keyword works in practice. Suppose we have a C# program that needs to call a function implemented in a native code library (in C or C++). We can define the function signature in our C# program using the “extern” keyword, as follows:
using System;
using System.Runtime.InteropServices;
class Program
{
? ? [DllImport("MyLibrary.dll")]
? ? extern static int MyFunction(int arg1, int arg2);
? ? static void Main(string[] args)
? ? {
? ? ? ? int result = MyFunction(10, 20);
? ? ? ? Console.WriteLine("Result = " + result);
? ? }
}
In the above code, we are using the “[DllImport]” attribute to specify the name of the DLL (“MyLibrary.dll”) that contains the implementation of the “MyFunction” method. We are also using the “extern” keyword to tell the C# compiler that the implementation of the “MyFunction” method is external to the current program.
领英推荐
At runtime, when the “MyFunction” method is called, the .NET runtime will load the DLL specified in the “[DllImport]” attribute and look for the implementation of the “MyFunction” method in that DLL. If the implementation is found, it will be executed, and the result will be returned to the calling C# program.
Another common use of the “extern” keyword is to define a “wrapper” method that calls a native code function. Let’s take a look at an example:
using System;
using System.Runtime.InteropServices;
class Program
{
? ? [DllImport("MyLibrary.dll")]
? ? extern static int NativeFunction(int arg1, int arg2);
? ? static int MyFunction(int arg1, int arg2)
? ? {
? ? ? ? // Call the native function using the wrapper method
? ? ? ? return NativeFunction(arg1, arg2);
? ? }
? ? static void Main(string[] args)
? ? {
? ? ? ? int result = MyFunction(10, 20);
? ? ? ? Console.WriteLine("Result = " + result);
? ? }
}
In the above code, we have defined a wrapper method called “MyFunction” that calls the native function “NativeFunction”. We are using the “extern” keyword to tell the C# compiler that the implementation of the “NativeFunction” method is external to the current program. At runtime, when the “MyFunction” method is called, it will call the “NativeFunction” method in the external DLL, and the result will be returned to the calling C# program.