简体   繁体   English

使用Public方法的私有类?

[英]Private class with Public method?

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 抱歉

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 . 你有一个公共类SomeClass ,你想在其中封装一些只与SomeClass相关的功能。 You could do this by declaring a private class ( SomePrivateClass in my example) within SomeClass , as shown below. 您可以通过在SomeClass声明一个私有类(在我的示例中为SomePrivateClass )来完成此操作,如下所示。

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. 无论SomePrivateClassstatic还是包含public static方法,都是如此。

I would call this a nested class , and it is explored in another StackOverflow thread . 我将其称为嵌套类 ,并在另一个StackOverflow线程中进行探索。

Richard Ev gave a use case of access inside a nested classes. Richard Ev在嵌套类中给出了一个访问用例。 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. 通过将MySpecialEnumerator类设置为内部,也可以在没有嵌套类的情况下完成此操作,因为您不能拥有非嵌套的私有类。

The BCL uses non-public implementations extensively. BCL广泛使用非公开实现。 For example, objects returned by LINQ operators are non-public classes that implement IEnumerable<T> . 例如,LINQ运算符返回的对象是实现IEnumerable<T>非公共类。

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. 可能不是,因为Main方法必须在public类中。

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. Main()方法是应用程序执行开始的地方,因此您无法编译第一个类(使用public static void Main() )的原因是因为您已经在应用程序的其他位置使用了Main方法。 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. 您的应用程序必须只有一个Main方法才能使用默认行为进行编译,否则您需要在编译时添加/ main选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM