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)
        {
        
        }
    }
  
}

Leave A Reply

Your email address will not be published. Required fields are marked *

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...