Nested class with hidden constructor impossible in c#?

Posted by luckyluke on Stack Overflow See other posts from Stack Overflow or by luckyluke
Published on 2010-03-13T21:56:22Z Indexed on 2010/03/13 22:05 UTC
Read the original article Hit count: 289

I' ve been doing some programming lately and faced an issue which i found weird in c#. (at least for me)

public class Foo
{
    //whatever
    public class FooSpecificCollection : List<Bar>
    {
         //implementation details
    }
    public FooSpecificCollection GetFoosStuff()
    {
         //return the collection
    }
}

I want the consumer of Foo to be able to obtain a reference to FooSpecificCollection, even perform some operations on it. Maybe even set it to some other property of Foo or smth like that, but not To be able to CREATE an instance of this class. (the only class that should be able to instatiate this collection should be Foo.

Is my request really that far-fetched? I know that people way smarter defined c# but shouldn't there be such an option that a parent class can create a nested class instance but nobody else can't.

So far I created a solution to make an abstract class, or interface available through the property and implement a concrete private class that is not available anywhere else.

Is this a correct way to handle such a situation.?

© Stack Overflow or respective owner

Related posts about c#

Related posts about programming-languages