can we access a private variable using an object

前端 未结 7 1881
一整个雨季
一整个雨季 2020-12-11 08:07

We cannot access a private variable of a class from an object, which is created outside the class, but it is possible to access when the same object is created inside the cl

7条回答
  •  旧时难觅i
    2020-12-11 08:50

    You Can Access the private variables in the following process also...

    namespace OOPSProj
    {    
        class Program
        {    
            private int i;    
            public void Methode1()
            {
                Program objprog2 = new Program();
                objprog2.i = 15;
                Console.WriteLine("i= " + objprog2.i);
            }    
            static void Main(string[] args)
            {
                Program objprog = new Program();
                objprog.i = 10;
                Console.WriteLine("i= " + objprog.i);
                objprog.Methode1();                
            }
        }    
        class Test
        {
            static void Main()
            {
                Program objproj3 = new Program();
                objproj3.Methode1();    
            }          
        }    
    }
    

提交回复
热议问题