Home
Options

Non-obvious compiler error message : CS0050 Inconsistent accessibility:

This is low priority as I have sorted my issue and if I ever come across this error again, I will know exactly what to do, but on the other hand I really, really don't want to admit how long I spent scratching my head trying to figure out what was happening here. It might have been me being extra stupid and I don't know whether anything can be done to make it more obvious, but if it can, it might help someone else in the future.

Situation. I have a query which defines a class and an extension method which references that class. The following is a trivial example

void Main()
{
    "Hello".ToTest().Dump();
}

public static class TestExt
{
    public static Test ToTest(this string input)
    {
        return new Test() { StringValue = input} ;
    }
}

public class Test
{
    public string StringValue { get;set;}
}

Everything works fine and the script compiles and runs etc.

Then I have another query which loads this query. e.g.

#load ".\test"

void Main()
{
    "Hello Again".ToTest().Dump();
}

Again it compiles and runs correctly.

But when I try to load test in another query it gives a compile error.

CS0050 Inconsistent accessibility: return type 'UserQuery.Test' is less accessible than method 'TestExt.ToTest(string)'

Since both extension method and class were public, the message did not make sense to me. And because the extension method worked in another query, I thought that perhaps I had defined another class with the same name in the query that caused the problem. I compiled the problem query without loading test and looked at the result in Ilspy and didn't see anything obvious. I added the load back in but removed my extension method and everything compiled (but I still didn't see the reason in ilspy). It was only when I changed my extension method not to refer to my class and look at the compiled result in Ilspy was I able to figure out what was going on.

The class Test is defined inside the UserQuery class whereas the class TestExt is outside it (see screenshot)

But my script was using a typed data context from my own assembly and so UserQuery was defined as

internal class UserQuery : global::MyDataContext

So, even though my Test class is public, UserQuery is internal and so Test is effectively internal.

And the solution was simply to change my extension method to be internal and everything works.

The reason it worked in other queries is because UserQuery is defined as public for queries without connections and for (at least some) connections where LinqPad builds its own data context.

Part of me thinks I should have been able to work this out quicker and I perhaps I would have if I have really trusted the compiler error message to be accurate (which it was).

Comments

Sign In or Register to comment.