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");
        }
    }
}

Leave A Reply

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...