C # using the same variable in subclass and base class

This might be a dumb question, but I'm new to C #, so please bear with me.

What happens if I name the variable the same in both the base class and subclass?

For instance:

class BaseClass01 { int x = 10; } class SubClass01 : BaseClass01 { int x = 20; public int Multiplicative(int a) { return x * a; } } 

if a = 10, the answer I received was 200.

Does this mean that the variable "int x" in BaseClass01 is different from "int x" in SubClass01? Can anyone provide an example illustrating the differences?

Thank you in advance for helping me trick this confusing concept of inheritance!

Edit:

Based on the comments below, I was messing around with the code and realized that when accessing methods from the base class "x", it is not transferred from the subclass:

 class BaseClass01 { int x = 10; public int Subtraction(int a) { return a - x; } } class SubClass01 : BaseClass01 { int x = 20; public int Multiplicative(int a) { x = x * a return x; } } private void button4_Click(object sender, EventArgs e) { SubClass01 sb = new SubClass01(); int answer1 = sb.Multiplicative(10); int answer2 = sb.Subtraction(answer1); } 

Subtraction () continues to use the value "x" from BaseClass01 (that is, x remains 10). Using a secure keyword completely fixes the problem.

Thanks for the explanation!

+4
source share
5 answers

Yes, these are different fields that have the same unqualified name.

You may think:

 public int Multiplicative(int a) { return x * a; } 

as:

 public int Multiplicative(int a) { return this.x * a; } 

To access BaseClass01.x , use the base keyword:

 public int Multiplicative(int a) { return base.x * a; } 

(You also need to make BaseClass01.x protected and mark SubClass01.x as new.)

+4
source

Yes, because x in the first class is private, and the drived class does not see it. But if you make it (in the base class) secure and use it in this way, the compiler generates a warning:

Warning SubClass01.x hides the inherited BaseClass01.x element. Use a new keyword if concealment is intended.

actually says you're new, so again another.

in this case, it is better to use the new keyword.

+7
source

Yes, at first both X are private, therefore they are available only in their class, but if x in the parent class wasnt private, we can get it with base.x in the subclass, the compiler will look for the variable in the first block declared

+1
source

Since they are both private, a subclass can only access its variable x .

0
source

There are two places in memory where int is stored, so yes, there are two (unrelated) variables declared here. If you want to declare only one variable and only a link in a subclass, omit the second declaration and make the first protected int x;

This helps to think about how the class is represented in memory as a record. Each field occupies its own memory space with either data (value types) or a pointer (reference types). In fact, what you type is what you get.

0
source

Source: https://habr.com/ru/post/1380252/


All Articles