Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to use different declared accessibilities in classes with C#

1 Answer

0 votes
using System;

public class Test {
    public static int publicInt;
    internal static int internalInt;
    private static int privateInt = 0;
    
    static Test() {
        // Test can access public or internal members in a public or private (or internal) nested class (C1, C2)
        C1.publicIntC1 = 1;
        C1.internalIntC1 = 2;
        C2.publicIntC2 = 3;
        C2.internalIntC2 = 4;

        // C2.privateInt = 7; // error CS0117: `Test.C2' does not contain a definition for `privateInt'
    }

    public class C1 {
        public static int publicIntC1 = 11;
        internal static int internalIntC1 = 22;
        private static int privateIntC1 = 33;
    }

    private class C2 {
        public static int publicIntC2 = 333;
        internal static int internalIntC2 = 444;
        private static int privateIntC2 = 555;
    }
}

class MainClass
{
    static void Main()
    {
        Test.publicInt = 3; // Access unlimited

        Test.internalInt = 4; // Access only in current assembly

        // Test.privateInt = 5; // error CS0122: `Test.privateInt' is inaccessible due to its protection level

        Test.C1.publicIntC1 = 77; // Access unlimited

        Test.C1.internalIntC1 = 88; // Access only in current assembly

        //Test.C1.privateIntC1 = 33; // error CS0122: `Test.C1.privateIntC1' is inaccessible due to its protection level

        // private class C2
        //Test.C2.publicIntC2 = 222; // error CS0122: `Test.C2' is inaccessible due to its protection level

        // private class C2
        //Test.C2.internalIntC2 = 888; // error CS0122: `Test.C2' is inaccessible due to its protection level

        // private class C2
        //Test.C2.privateIntC2 = 777; // error CS0122: `Test.C2' is inaccessible due to its protection level
    }
}



/*
run:



*/

 



answered Nov 28, 2020 by avibootz
...