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?

thanks
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| 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 message says it all. Classes can only be declared as private, protected
or protected internal when declared as nested classes, other than that, it
doesn't make sense to declare a class with a visibility that makes it
unusable, even in the same module.

Joanna
 
B

Barry Kelly

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?

Private and protected classes must be nested inside another class.

What would it mean for a class to be private, yet declared at the
namespace level? How could any code access it?

-- Barry
 
C

Cowboy \(Gregory A. Beamer\)

If the class is private, that means no other class can use it. Period!

If you want to stop public instantiation, use this pattern instead:

public class Class1
{
private Class1()
{
}
int i;
}

If you want it only used internally, try "internal" instead of public.

If you want a private class, the class must be inside of another class.

public class SomeContainingClass
{
private class Class1
{
int i;
}
}


--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
C

cody

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?

thanks

top level classes cannot be private, they are "internal" by default, you
can just make them public to make them visible from outside your dll.
 
J

Jon Skeet [C# MVP]

Cowboy (Gregory A. Beamer) said:
If the class is private, that means no other class can use it. Period!

<pedant mode>
Ónly if it's a top level class (as it was in the OP's question.)

The code below shows a private class being used within the body of the
class it is nested in:

using System;

class Test
{
static void Main()
{
PrivateClass pc = new PrivateClass();
}

private class PrivateClass
{
internal PrivateClass()
{
Console.WriteLine ("Hi there!");
}
}
}
</pedant mode>

Time for bed when I'm getting that pedantic, I think :)
 

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