Compiler inconsistent accessibility error with passing nested class as a function parameter

My visual studio 2010 finds inconsistent accessibility of parameter type error when I compile this C# code:

class Program
{
    class BaseClass
    {
        public class NestedClass
        {

        }
    }

    public static bool Function1(BaseClass.NestedClass obj)
    {
        return true;
    }

    static void Main(string[] args)
    {
        Function1(new BaseClass.NestedClass());
        new BaseClass.NestedClass();
        Console.ReadLine();
    }
}

but when I comment function1 it works.

class Program
{
    class BaseClass
    {
        public class NestedClass
        {

        }
    }

    //public static bool Function1(BaseClass.NestedClass obj)
    //{
    //    return true;
    //}

    static void Main(string[] args)
    {
        //Function1(new BaseClass.NestedClass());
        new BaseClass.NestedClass();
        Console.ReadLine();
    }
}

Why NestedClass can be created but can't be parameter?

EDIT: Sorry, I wasn't quite precise with my question. Above I tried to recreate problem in Console application, but in my project I have other structure of classes:

         class BaseClass
            {
               public class NestedClass
                {

                }
            }

    public class OtherClass
    {
          public void Function1(BaseClass.NestedClass param)    
                {
                  var newObj = new BaseClass.NestedClass();
                }
    }

PS: Setting BaseClass to public really solved my problem. Now, thanks to answers and comments, I understand that it's because public Function1 has greater accessibility than internal (by default) BaseClass. But I don't understand why I can create new NestedClass objects without error?

Jon Skeet
people
quotationmark

Top-level types default to being internal, but nested types default to being private. You've therefore got an internal type with a public method that has a private type as a parameter. Making all the access modifiers explicit:

internal class Program
{
    private class BaseClass
    {
        public class NestedClass {}
    }

    public static bool Function1(BaseClass.NestedClass obj)
    {
        return true;
    }

    private static void Main(string[] args)
    {
        Function1(new BaseClass.NestedClass());
        new BaseClass.NestedClass();
        Console.ReadLine();
    }
}

That means any code in the rest of the assembly could "see" Function1 but not understand it - hence the inconsistent accessibility error.

You can fix this by either making BaseClass internal or public or by making Function1 private. Note that BaseClass doesn't have to be public just because Function1 is public, because the accessibility domain of Program is just the assembly containing it. That makes the accessibility domain of Function1 the same. See section 3.5.2 of the C# spec for more details.

Personally I would avoid using nested classes too heavily - and by the time you have a nested type within a nested type, I would strongly consider a redesign.

people

See more on this question at Stackoverflow