4

I've recently started learning C# and I'm confused about something. The documentation for static classes tells me that they can only contain static members. Yet I can define non-static nested classes and structs within my static class.

I'm guessing that class/struct definitions don't count as members, but why is this allowed? If a nested class of a static class can be instantiated, doesn't that contradict the point of a static class? Am I misunderstanding something obvious here?

3
  • Bad wording on my part, I meant to say nested class. Commented Oct 5, 2017 at 10:19
  • I would say that you miss the obvious... A static class main purpose is to prevent the instanciation of that class. Commented Oct 5, 2017 at 11:03
  • I consider a nested class to be a static member of its enclosing class, regardless of whether or not it's a static class. (This is unlike Java, where nested classes are not always static members of the enclosing class, and the term "static class" means a nested class which is a static member.) Commented Oct 5, 2017 at 13:22

2 Answers 2

7

In C# nested classes are not subclasses, the surrounding class is more like another namespace. You dont have access to an instance of the outer class from within the inner class(as opposed to f.e. Java). That's why static classes can contain nested types.

A famous example, the LINQ class Enumerable which is static. It contains many helper classes:

public static class Enumerable
{
    // many static LINQ extension methods...

    class WhereEnumerableIterator<TSource> : Iterator<TSource>
    {
       // ...
    }

    internal class EmptyEnumerable<TElement>
    {
        public static readonly TElement[] Instance = new TElement[0];
    }

    public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, ILookup<TKey, TElement>
    {
        // ...
    }

     // many others
}

So the surrounding static class is a logical container for the inner class. It belongs there because it is used from the static class and often not accessible from somewhere else(if not public).

But you're right, it's a lack of documentation. They should have said:

Contains only static members or nested types

Sign up to request clarification or add additional context in comments.

4 Comments

In a sense, a (nested) class definition is something static.
@Haukinger: No, you can create multiple instances: var i1 = new OuterC.InnerC(); var i2 = new OuterC.InnerC();(if it's accessible)
Sure thing, the type defined may be static or not, but the defnition is static. You don't write var x = new OuterC(); var y = new x.InnerC();...
@Haukinger: that's what i meant with "more like a namespace" and "logical container". The term static is just misleading here.
3

The documentation is a little lacking, but nested classes/structs are allowed in static classes, and can also be static, or can be instantiated. Consider the following code:

namespace StaticClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            new Foo(); // Cannot create an instance of the static class 'Foo'
            new Foo.Bar(); // Cannot create an instance of the static class 'Foo.Bar'
            new Foo.Baz();
        }
    }

    static class Foo
    {
        public static class Bar
        {

        }

        public class Baz
        {

        }
    }
}

In this context, static classes are similar to namespaces, but namespaces (probably) describe the semantic relationships better than nested classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.