Using base to access a hidden name

F

Fons

In the fragment below, I can use 'base' in getbaseval() to get the
hidden value of 'a' in the role of a member of class A. Is it possible to
access this value directly, something like the (incorrect) attempt I made?


class A {
public int a=1;}
class B:A {
new public int a=2;
public int getval(){return a;}
public int getbaseval() {return base.a;}}
class Demo {
public static void Main(){
B ob = new B();
Console.WriteLine(ob.getval());
Console.WriteLine(ob.getbaseval());
Console.WriteLine(ob.a);
//Console.WriteLine(ob.base.a);
}}
 
N

nick.fletcher

You can cast B to its base type A and then access the property

((A)B).a.ToString();
or

(B as A).a.ToString()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top