Kiki
Kiki

Reputation: 9

Create new instance of private class in another private class

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?

Upvotes: 0

Views: 4010

Answers (3)

bergler77
bergler77

Reputation: 372

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.

Upvotes: 1

eugenioy
eugenioy

Reputation: 12393

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();
    }
}

Upvotes: 2

Idle_Mind
Idle_Mind

Reputation: 39142

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;
            }
        }

    }
}

Upvotes: 0

Related Questions