c# - Cannot access protected member? -


i learning c#, written below code not working.

the protected members can accessble inheriting class, in below code not working can 1 please tell me doing mistake?

   class protecteddemo {     protected string name;      public void print()     {         console.writeline("name is: {0}", name);     } }  class demo : protecteddemo {     static void main(string[] args)     {         protecteddemo p = new protecteddemo();         console.write("enter name:");         p.name = console.readline(); //here getting error.         p.print();         console.readline();     } } 

from protected (c# reference)

the protected keyword member access modifier. protected member accessible within class , derived class instances.

so comment, accessible within protecteddemo or inheriting class demo

and example

class {     protected int x = 123; }  class b : {     static void main()     {         a = new a();         b b = new b();          // error cs1540, because x can accessed         // classes derived a.         // a.x = 10;           // ok, because class derives a.         b.x = 10;     } } 

so change class to

class demo : protecteddemo {     static void main(string[] args)     {         //protecteddemo p = new protecteddemo();         demo p = new demo(); //note here         console.write("enter name:");         p.name = console.readline(); //here getting error.         p.print();         console.readline();     } } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -