Why is it that C# namespace can't have any but public elements?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Why is it that C# namespace can't have any but public elements?

  1. #1
    Thomas Eyde Guest

    Why is it that C# namespace can't have any but public elements?

    Why is it that C# namespace can't have any but public elements? How do I
    declare a private class?

    ..thomas




  2. #2
    Len Weaver Guest

    Re: Why is it that C# namespace can't have any but public elements?

    Hello Tom,

    > Why is it that C# namespace can't have any but
    > public elements? How do I declare a private class?


    You can't. Your classes can be internal though.

    Hope this helps,
    Len



  3. #3
    Ovidiu Platon Guest

    Re: Why is it that C# namespace can't have any but public elements?

    AFAIK, a namespace can have public or internal data types. You can declare
    private classes inside other types, if that's what you're looking for.
    HTH,
    Ovidiu.

    "Thomas Eyde" <thomas.eyde@online.no> wrote in message
    news:3d10e709@10.1.10.29...
    > Why is it that C# namespace can't have any but public elements? How do I
    > declare a private class?
    >
    > .thomas
    >
    >
    >




  4. #4
    Thomas Eyde Guest

    Re: Why is it that C# namespace can't have any but public elements?

    I am confused. One thing is the documentation does not say that classes at
    namespace level can only be declared as public or internal. The docs say
    public, internal, protected and private are allowed, no exception.

    But I don't understand the need of a new modifyer, either. One should think
    that for a namespace, internal would be equal to private. And since private
    is not allowed, why introduce a new keyword instead of reusing the existing
    ones?

    Declaring private classes inside another class(es) only to be able to use
    the private keyword is a little too stupid.

    ..thomas

    "Ovidiu Platon" <ovidiupl@microsoft-lab.pub.ro> wrote in message
    news:3d119ecc@10.1.10.29...
    > AFAIK, a namespace can have public or internal data types. You can declare
    > private classes inside other types, if that's what you're looking for.
    > HTH,
    > Ovidiu.





  5. #5
    David Bayley Guest

    Re: Why is it that C# namespace can't have any but public elements?

    Thomas Eyde wrote:

    > I am confused. One thing is the documentation does not say that
    > classes at namespace level can only be declared as public or
    > internal. The docs say public, internal, protected and private are
    > allowed, no exception.


    From the C# Language Reference, 3.3.1 "Declared accessibility"

    <quote>
    Types declared in compilation units or namespaces can have public or
    internal declared accessibility and default to internal declared
    accessibility.

    Class members can have any of the five types of declared accessibility
    and default to private declared accessibility. (Note that a type
    declared as a member of a class can have any of the five types of
    declared accessibility, whereas a type declared as a member of a
    namespace can have only public or internal declared accessibility.)
    </quote>

    Here, nested classes are grouped under "class members", and can have any
    of the five access modifiers. For root level classes (or more
    generically, "types"), access is explicitly limited to public or
    internal.

    > But I don't understand the need of a new modifyer, either. One should
    > think that for a namespace, internal would be equal to private. And
    > since private is not allowed, why introduce a new keyword instead of
    > reusing the existing ones?


    The internal modifier is not just for classes, it is also used for class
    members (methods, props, nested classes), to limit access to the
    assembly ("compilation unit"). The choice of internal or private for
    root-level classes is debatable, but I think internal is more
    appropriate.

    > Declaring private classes inside another class(es) only to be able to
    > use the private keyword is a little too stupid.


    But for nested classes, you can use *either* internal or private, and
    the accessibility is different for each one. It is only with root level
    classes where there is no difference.

    --
    David




  6. #6
    Thomas Eyde Guest

    Re: Why is it that C# namespace can't have any but public elements?

    Thanks David,

    I didn't see this information when marking "class" and hitting F1.

    ..thomas



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center