HomeCSharpC# Error CS0601 – The DllImport attribute must be specified on a method marked ‘static’ and ‘extern’

C# Error CS0601 – The DllImport attribute must be specified on a method marked ‘static’ and ‘extern’

C# Error

CS0601 – The DllImport attribute must be specified on a method marked ‘static’ and ‘extern’

Reason for the Error & Solution

The DllImport attribute must be specified on a method marked ‘static’ and ‘extern’

The DllImport attribute was used on a method that did not have the correct access keywords.

The following sample generates CS0601:

// CS0601.cs  
using System.Runtime.InteropServices;  
using System.Text;  
  
public class C  
{  
   [DllImport("KERNEL32.DLL")]  
   extern int GetCurDirectory(int bufSize, StringBuilder buf);   // CS0601  
   // Try the following line instead:  
   // static extern int GetCurDirectory(int bufSize, StringBuilder buf);  
}  
  
public class MainClass  
{  
   public static void Main ()  
   {  
   }  
}  

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...