473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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?

Jun 19 '06 #1
4 6214
<ne*******@gmail.com> a écrit dans le message de news:
11*********************@h76g2000cwa.googlegroups.c om...

Answered in dotnet.general.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 19 '06 #2
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
}

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"ne*******@gmail.com" wrote:
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?

Jun 19 '06 #3
<ne*******@gmail.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
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
Jun 19 '06 #4
<ne*******@gmail.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
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
Jun 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xavier Decoret | last post by:
The following code does not compoile with gcc-3.2.3 namespace dummy { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Interface of Foo...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
7
by: My4thPersonality | last post by:
1st, simple question, is there a protected in C#? I mean a comparable keyword like the C++ protected, which defines a member as accessable for decendants, but not for the outside world. Then,...
3
by: Asfand Yar Qazi | last post by:
For years, I've been putting everything that won't be needed outside a compilation unit in anonymous namespaces, even editing my old files that did things the 'old' way (using static linkage). ...
5
by: newbie120 | last post by:
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;
2
by: Mark | last post by:
Hi... I've been trying the .Validate() method on the XmlDocument to validate some xml against a schema, but one thing I noted was that unless the document explicitly declares the schema as a...
5
by: JamesO | last post by:
Hello all, Is there a way to make a class private to the namespace so that other classes in the namespace can see and use it but noone outside a namespace can see or use it? Feel free to send...
9
by: vthomasset | last post by:
Hi, Sorry for the bad subject, but i couldn't figure a better one. I would like to understand why a variable declared non static in a header causes multiple symbol definitions (if included in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.