Namespace elements cannot be explicitly declared as private, protected, or protected internal

N

newbie120

Hi all

maybe its just been a long day, but i have a question about call access
modifiers in C#. Consider the following code.

namespace Application
{
private class Class1
{
int i;
}
}

When I try to compile this i get the following error:
"Namespace elements cannot be explicitly declared as private,
protected, or protected internal"

When i make Class1 public i no longer get a compile error. Now correct
me if i'm wrong, but doesn't C# allow me to have private or protected
class structures? Am i missing something?
 
G

Guest

From MSDN documentation:

Namespace elements cannot be explicitly declared as private, protected, or
protected internal

Type declarations in a namespace can have either public or internal 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
}
 
C

Chris Jobson

Hi all

maybe its just been a long day, but i have a question about call access
modifiers in C#. Consider the following code.

namespace Application
{
private class Class1
{
int i;
}
}

When I try to compile this i get the following error:
"Namespace elements cannot be explicitly declared as private,
protected, or protected internal"

When i make Class1 public i no longer get a compile error. Now correct
me if i'm wrong, but doesn't C# allow me to have private or protected
class structures? Am i missing something?

The definition of private is "Private members are accessible only within the
body of the class or the struct in which they are declared", so I guess it
makes no sense to declare something private if it's not within a class or
struct. Similarly protected only makes sense for a class member (but not a
struct member, since a struct cannot inherit from another struct). Thus I
think you can have private or protected classes, but only if they are nested
within another class/struct.

I also found this in the help: "Classes and structs that are not nested
within other classes or structs can be either public or internal. A type
declared as public is accessible by any other type. A type declared as
internal is only accessible by types within the same assembly. Classes and
structs are declared as internal by default unless the keyword public is
added to the class definition".

Chris Jobson
 
C

Chris Jobson

Hi all

maybe its just been a long day, but i have a question about call access
modifiers in C#. Consider the following code.

namespace Application
{
private class Class1
{
int i;
}
}

When I try to compile this i get the following error:
"Namespace elements cannot be explicitly declared as private,
protected, or protected internal"

When i make Class1 public i no longer get a compile error. Now correct
me if i'm wrong, but doesn't C# allow me to have private or protected
class structures? Am i missing something?

The definition of private is "Private members are accessible only within the
body of the class or the struct in which they are declared", so I guess it
makes no sense to declare something private if it's not within a class or
struct. Similarly protected only makes sense for a class member (but not a
struct member, since a struct cannot inherit from another struct). Thus I
think you can have private or protected classes, but only if they are nested
within another class/struct.

I also found this in the help: "Classes and structs that are not nested
within other classes or structs can be either public or internal. A type
declared as public is accessible by any other type. A type declared as
internal is only accessible by types within the same assembly. Classes and
structs are declared as internal by default unless the keyword public is
added to the class definition".

Chris Jobson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top