C# Error CS0179 – ‘member’ cannot be extern and declare a body

C# Compiler Error

CS0179 – ‘member’ cannot be extern and declare a body

Reason for the Error

You will receive this error when you have declared a class member as extern but also contains the definition of the member. In C#, when mark a class member as extern, you cannot have the member definition in the same class.

For example, lets try to compile the below code snippet.

namespace DeveloperPubNamespace
{
    public class Class1
    {
        // This results with the error code S0179  
        public extern int Method1(int input)  
        {
            return 0;
        }  

        public static void Main()
        {
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
        
        }
    }
  
}

The above code snippet will result with the error code CS0179 because the Method1 is marked as extern but contains the definition or method body.

Error CS0179 ‘Class1.Method1(int)’ cannot be extern and declare a body ConsoleApp3 C:\Users\SenthilBalu\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 7 Active

Solution

To fix the error, remove the extern keyword or remove the definition of the member. For example, the error can be fixed in the above code snippet by removing the definition as shown below.

namespace DeveloperPubNamespace
{
    public class Class1
    {
        // This results with the error code S0179  
        public extern int Method1(int input);
    }
    class Program
    {
        static void Main(string[] args)
        {
        
        }
    }
  
}