The extern Keyword in C#

Muhammad Zeeshan Oct 12, 2023
  1. extern in C#
  2. How extern Works in C#
The extern Keyword in C#

In today’s article, we’ll learn the functionality of the extern keyword in C#.

extern in C#

Declaring an externally implemented method is done using the extern modifier. When utilizing Interop services to call into unmanaged code, the extern modifier is often used in conjunction with the DllImport attribute.

It is necessary to define the method as static in this situation.

The extern keyword may also be used to construct an external assembly alias, enabling several versions of the same component to be referenced inside a single assembly.

To alter a single member using both the abstract and extern modifiers simultaneously is unacceptable. When you use the abstract modifier, it indicates that the method implementation is not supplied in the class, in contrast to when you use the extern modifier, which suggests that the method implementation is located outside the C# code.

How extern Works in C#

It is agreed upon that a method is considered an external method if its declaration contains the extern modifier. The implementation of external methods often takes place outside C# and uses a different programming language.

Since an external method declaration does not include any actual implementation, the method body of an external method comprises a single semicolon. There is a possibility that an exterior method is not generic.

The extern modifier is often used in conjunction with a DllImport property. This combination allows Dynamic Link Libraries or DLLs to incorporate methods from external modules.

There is a possibility that the execution environment will support additional techniques that make it possible to offer implementations of external methods. When an external method has a DllImport property, the method declaration must also have a static modifier to function correctly.

Syntax:

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

Example:

In this example, we may utilize extern aliases to import functionality and then use that functionality as appropriate.

A single fully qualified name may refer to two types when using an external alias. When two entities have the same fully qualified name, indicating the namespace and type names are the same.

This is something that can only take place when the two kinds originate from two separate assemblies.

  1. First library compiled to info1.dll

    namespace information {
      public class info {}
    }
    
  2. Second library compiled to info2.dll

    namespace information {
      public class info {}
    }
    
  3. The following is an example of an application that uses the info1.dll and info2.dll files.

    using info;
    class Test {
      static void Main() {
        information i = new information();
      }
    }
    
  1. Due to the lack of clarity in the information, the application cannot be compiled. In this situation, the uncertainty may be cleared using extern aliases.

    You first need to change the .csproj file the application uses and give each reference its distinct alias.

    <ItemGroup>
    <Reference Include="info1">
    <Aliases>i1</Aliases>
    </Reference>
    <Reference Include="info2">
    <Aliases>i2</Aliases>
    </Reference>
    </ItemGroup>
    
  2. Utilize the extern alias directive as the last step in the process.

    extern alias i1;
    extern alias i2;
    
    class Test {
      static void Main() {
        i1.information.info first = new i1.information.info();
        i2.information.info second = new i2.information.info();
      }
    }
    
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn