I have this below piece of code which makes me puzzling about internals of a private class. I could see many search results for this error, still the below sounds wierd
namespace X
{
public class Program
{
public static XYZ sample1;
public static void Main(string[] args)
{
XYZ sample2 = new XYZ(); // OK (1)
sample1 = new XYZ(); // NOK (2)
...
}
}
private class XYZ
{
}
}
If class XYZ is private, how does it work at (1) but not in (2)?
It works in both (1) and (2):
You can’t declare
public static XYZ sample1;at all because it’s public when XYZ is private:This makes sense, users of
X.Programwill have access to its public members (in this casesample1) but they won’t be able to use them becauseXYZis private to the assembly (then not accessible). That’s why the compiler says “inconsistent accessibility”.