C# Error CS1527 – Elements defined in a namespace cannot be explicitly declared as private, protected, protected internal, or private protected

C# Error

CS1527 – Elements defined in a namespace cannot be explicitly declared as private, protected, protected internal, or private protected

Reason for the Error & Solution

Elements defined in a namespace cannot be explicitly declared as private, protected, protected internal or private protected.

Type declarations in a namespace can have either or access. If no accessibility is specified, internal is the default.

The following sample generates CS1527:

// CS1527.cs  
namespace Sample  
{  
   private class C1 {}             // CS1527  
   protected class C2 {}           // CS1527  
   protected internal class C3 {}  // CS1527  
   private protected class C4 {}   // CS1527
}  

The following example generates CS1527 because when no namespace is explicitly declared in your program code, all type declarations are located implicitly within the global namespace.

//cs1527_2.cs  
using System;  
  
protected class C4 {}  
private struct S1 {}  

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