Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0267 – The partial modifier can only appear immediately before ‘class’, ‘record’, ‘struct’, ‘interface’, or a method return type

C# Compiler Error

CS0267 – The partial modifier can only appear immediately before ‘class’, ‘record’, ‘struct’, ‘interface’, or a method return type

Reason for the Error

You’ll get this error in your C# code when you use have used the in-correct placement of the partial modifier when you are declaring the class, record, struct or method.

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

using System;

namespace DeveloperPubNamespace
{
    partial public class Employee
    {

    }
    class Program
    {
        public static void Main()
        {        
            Console.WriteLine("No Error");
        }
    }
}

You’ll receive the error code CS0267 when you try to build the above C# program because you are using the keyword “partial” before the “public” access modifier when declaring the Employee class.

Error CS0267 The ‘partial’ modifier can only appear immediately before ‘class’, ‘record’, ‘struct’, ‘interface’, or a method return type. DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 5 Active

Solution

You can fix this error in your C# program by ensuring that you re-order the placement of the modifiers as shown below.

using System;

namespace DeveloperPubNamespace
{
    public partial class Employee
    {

    }
    class Program
    {
        public static void Main()
        {        
            Console.WriteLine("No Error");
        }
    }
}