我试图了解c#中的嵌套类。我理解嵌套类是在另一个类中定义的类,我不明白的是为什么我需要这样做。


当前回答

其目的通常只是限制嵌套类的范围。与普通类相比,嵌套类具有私有修饰符的额外可能性(当然也是受保护的)。

Basically, if you only need to use this class from within the "parent" class (in terms of scope), then it is usually appropiate to define it as a nested class. If this class might need to be used from without the assembly/library, then it is usually more convenient to the user to define it as a separate (sibling) class, whether or not there is any conceptual relationship between the two classes. Even though it is technically possible to create a public class nested within a public parent class, this is in my opinion rarely an appropiate thing to implement.

其他回答

也许这是一个何时使用嵌套类的好例子?

// ORIGINAL
class ImageCacheSettings { }
class ImageCacheEntry { }
class ImageCache
{
    ImageCacheSettings mSettings;
    List<ImageCacheEntry> mEntries;
}

And:

// REFACTORED
class ImageCache
{
    Settings mSettings;
    List<Entry> mEntries;

    class Settings {}
    class Entry {}
}

PS:我没有考虑应该应用哪些访问修饰符(私有,受保护,公共,内部)

有时候,实现一个将从类内部返回的接口是有用的,但该接口的实现应该完全对外界隐藏。

举个例子,在将yield添加到c#之前,实现枚举器的一种方法是将枚举器的实现作为一个集合中的私有类。这将为访问集合的成员提供方便,但外部世界不需要/看到这是如何实现的细节。

公共嵌套成员也有很好的用途…

嵌套类可以访问外部类的私有成员。因此,在一个场景中,这是正确的方式将是创建一个比较器(即。实现IComparer接口)。

在这个例子中,FirstNameComparer可以访问私有的_firstName成员,如果类是一个单独的类,它就不能访问。

public class Person
{
    private string _firstName;
    private string _lastName;
    private DateTime _birthday;

    //...
    
    public class FirstNameComparer : IComparer<Person>
    {
        public int Compare(Person x, Person y)
        {
            return x._firstName.CompareTo(y._firstName);
        }
    }
}

其目的通常只是限制嵌套类的范围。与普通类相比,嵌套类具有私有修饰符的额外可能性(当然也是受保护的)。

Basically, if you only need to use this class from within the "parent" class (in terms of scope), then it is usually appropiate to define it as a nested class. If this class might need to be used from without the assembly/library, then it is usually more convenient to the user to define it as a separate (sibling) class, whether or not there is any conceptual relationship between the two classes. Even though it is technically possible to create a public class nested within a public parent class, this is in my opinion rarely an appropiate thing to implement.

我特别喜欢的一个模式是将嵌套类与工厂模式结合起来:

public abstract class BankAccount
{
  private BankAccount() {} // prevent third-party subclassing.
  private sealed class SavingsAccount : BankAccount { ... }
  private sealed class ChequingAccount : BankAccount { ... }
  public static BankAccount MakeSavingAccount() { ... }
  public static BankAccount MakeChequingAccount() { ... }
}

通过像这样嵌套类,我使第三方不可能创建他们自己的子类。我可以完全控制在任何bankaccount对象中运行的所有代码。我的所有子类都可以通过基类共享实现细节。