Private class with Public method?
Asked Answered
H

4

10

Here is a piece of code:

private class myClass
{
   public static void Main()
   {

   }
}

        'or'

private class myClass
{
   public void method()
   {

   }
}

I know, first one will not work. And second one will.

But why first is not working? Is there any specific reason for it?

Actually looking for a solution in this perspective, thats why made it bold. Sorry

Help answered 15/10, 2011 at 10:35 Comment(6)
Hard to guess at what you mean, neither is valid. Only a nested class can be private. A non-nested class can't be private because then it can never be used by any code.Tiernan
@rapsalands - what specifically do you mean by "will not work"?Sick
@Hans: Why it is not used by any one? Private class can be accessed with in namespace, can be instantiated and public members can be accessed through itHelp
@Richard: It will not compileHelp
No. The compiler is pretty clear about it: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internalTiernan
@Hans: Thanks, you are right. I misunderstood few things. Thaks for bearing with me. None of them will work.Help
S
15

It would be meaningful in this scenario; you have a public class SomeClass, inside which you want to encapsulate some functionality that is only relevant to SomeClass. You could do this by declaring a private class (SomePrivateClass in my example) within SomeClass, as shown below.

public class SomeClass
{
    private class SomePrivateClass
    {
        public void DoSomething()
        {

        }
    }

    // Only SomeClass has access to SomePrivateClass,
    // and can access its public methods, properties etc
}

This holds true regardless of whether SomePrivateClass is static, or contains public static methods.

I would call this a nested class, and it is explored in another StackOverflow thread.

Sick answered 15/10, 2011 at 10:42 Comment(4)
Only SomeClass has access to [...] its public methods. - not quite accurate. If you give a reference of the nested private class to someone outside of the outer class, it will have access to those public methods too (see my answer for an example).Spires
Good point made by Allon. However, in this situation you're supplying a reference to an instance of a private class that implements a public interface. So the "someone outside" is accessing the private class via the supplied interface, it has no explicit knowledge of the private class itself.Sick
What can access DoSomething?Chiasmus
@Chiasmus - anything inside SomeClass that has access to an instance of SomePrivateClass will be able to call DoSomething on that instance.Sick
S
3

Richard Ev gave a use case of access inside a nested classes. Another use case for nested classes is private implementation of a public interface:

public class MySpecialCollection<T> : IEnumerable<T>
{ 
    public IEnumerator<T> GetEnumerator()
    {
        return new MySpecialEnumerator(...);
    }

    private class MySpecialEnumerator : IEnumerator<T>
    {
        public bool MoveNext() { ... }
        public T Current
        { 
            get { return ...; }
        }
        // etc...
    } 
}

This allows one to provide a private (or protected or internal) implementation of a public interface or base class. The consumer need not know nor care about the concrete implementation. This can also be done without nested classes by having the MySpecialEnumerator class be internal, as you cannot have non-nested private classes.

The BCL uses non-public implementations extensively. For example, objects returned by LINQ operators are non-public classes that implement IEnumerable<T>.

Spires answered 15/10, 2011 at 10:50 Comment(0)
S
1

This code is syntactically correct. But the big question is: is it useful, or at least usable in the context where you want to use it? Probably not, since the Main method must be in a public class.

Stocktonontees answered 15/10, 2011 at 10:38 Comment(0)
M
0

Main() method is where application execution begin, so the reason you cannot compile your first class (with public static void Main()) is because you already have Main method somewhere else in your application. The compiler don't know where to begin execute your application.

Your application must have only one Main method to compile with default behavior otherwise you need to add /main option when you compile it.

Maleficence answered 15/10, 2011 at 11:5 Comment(4)
thanks dude, but I don't have 2 Main methods. I modifies the one in Program.cs. The issue here is private class not method name. Still thanks.Help
What's the type of your project, application or library? It will be helpful if you add all code to your question though. :)Maleficence
private class is not problem here.Maleficence
its application, in library whats the need of Main methodHelp

© 2022 - 2024 — McMap. All rights reserved.