0

Is it possible to create an instance of a private class in another private class? (Not counting within the main() program.) And also, is it possible for a method in a private class to return a private type object?

This question came because I was following Scott Allen from PluralSight on C# Fundamentals With C#5. And on lesson 2 about classes and objects, he has a code example like this:

public GradeStatistics ComputeStatistics()
{
    GradeStatistics stats = new GradeStatistics();
    ...
    ...
}

with GradeStatistics defined in a separate class file like:

class GradeStatisticss
{

}

Inlined comment: I am not talking about nested classes. What I meant is, you have two classes (separate files) and I am wondering if one class can create an instance of another class (knowing they are both private).

Edited with examples:

    private class Example1
    {

    }

    private class Example2

    {
        public Example1 DoSomeComputation()
        {
            return new Example1();
        }
    }

    private class Example3
    {
        Example1 ex1 = new Example1();
    }

Is Example3 able to create ex1? Can Example2 return a new instance of Example1?
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Kiki
  • 9
  • 1
  • 5
  • You should ask these two questions separately. – Nathan Tuggy Jun 30 '15 at 02:05
  • Kiki, please make sure to edit your post to align it with your comment (I've inlined your comments as initial version, but you need to edit it to make it clear). – Alexei Levenkov Jun 30 '15 at 02:13
  • thanks Alexei. Added examples – Kiki Jun 30 '15 at 02:21
  • -1 `private class` modifier can not be used outside of a class. Without seeing the nesting of the container class this question cannot be answered. Separate file be damned (hello `public partial class`) – Aron Jun 30 '15 at 02:30
  • I wonder why you are -1 my question. There is no stupid question. The stupid question is the one that is not aked.... And I am not pretending to be an expert. – Kiki Jun 30 '15 at 02:34
  • -1 means "shows no research effort/unclear" - so you hit on both points based on your last edit: there is no private types at all in problem you described, you did not try to find what are http://stackoverflow.com/questions/2521459/what-are-the-default-access-modifiers-in-c and did not try to actually compile code that you think you have (where `private class` would immediately fail). Thanks for updating your post and finally revealing actual question, but I'm afraid there is not much to answer at this point. – Alexei Levenkov Jun 30 '15 at 07:36

3 Answers3

2

Is it possible to create an instance of a private class in another private class?

Only if the private class for which you want to create an instance is declared inside the private class that wants to create the instance. If they are not nested, it's not possible.

Is it possible for a method in a private class to return a private type object?

Yes, it can.

Here's some code showing everything together:

public class Tester {

    private class ThePrivateCreator {

        private class TheOtherPrivateClass {
        }

        public Object createObject() {
            return new TheOtherPrivateClass();
        }

    }

    public void canWeDoThis() {
        ThePrivateCreator c = new ThePrivateCreator();
        Console.WriteLine(c.createObject());
    }

}


class Program
{
    public static void Main(string[] args) {
        Tester t = new Tester();
        t.canWeDoThis();
    }
}
eugenioy
  • 11,825
  • 28
  • 35
1

No. A private class cannot be accessed by another class in a different file. The reason why is that the modifier private is meant to encapsulate the data or method inside of that class. You should use the public or internal modifier if you want to access a class from a different class that is not nested. If it is nested, you can also use the protected modifier.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
bergler77
  • 362
  • 1
  • 3
  • 13
0

Not sure exactly what you had in mind, but here's one possible example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication26
{
    class Program
    {

        static void Main(string[] args)
        {
            private1 p1 = new private1();
            private2 p2 = p1.foo();
            Console.WriteLine(p2.Value);
            Console.ReadLine();
        }

        private class private1
        {

            public private2 foo()
            {
                private2 p2 = new private2("I was created inside a different private class!");
                return p2;
            }

        }

        private class private2
        {

            private string _value;
            public string Value
            {
                get { return _value; }
            }

            public private2(string value)
            {
                this._value = value;
            }
        }

    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • thanks for you input. But that was not my question. In you example, can private2 create and instance of private1. And I explicitly said not in the Main(). – Kiki Jun 30 '15 at 02:11
  • In foo(), private1 is creating an instance of private2. Isn't that what you were asking?...it can certainly be switched around, but why? – Idle_Mind Jun 30 '15 at 02:39
  • Idle_Mind. I would have marked it as an answer if you can please clear it for me. I did not quite read your answer very well the first time. You defined your 2 classes (Private1 and Private2) in the same Program class. What if Private1 and Private2 were separate files (not under Program)? would your example still stand? Plus, what would happened if you did not have the public constructor in Private2 class? – Kiki Jun 30 '15 at 02:58