Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:11:01+00:00 2026-05-26T16:11:01+00:00

This might be a silly question, but I’m a beginner in C#, so please

  • 0

This might be a silly question, but I’m a beginner in C#, so please bear with me.

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

For example:

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 got was 200.

Does this mean variable “int x” in BaseClass01 is different from “int x” in SubClass01? Would anybody be able to provide an example that illustrate the differences?

Thanks in advance for helping me wrap my head around this confusing concept of inheritance!

Edit:

Based on the comments below, I tinkered with the code, and realized that when accessing methods from the base class, the “x” from the subclass does not carry over:

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 “x” value from BaseClass01 (i.e. x remains 10). Using protected keyword avoids the issue entirely.

Thanks for the explanations!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:11:01+00:00Added an answer on May 26, 2026 at 4:11 pm

    Yes they are different fields that happen to have the same unqualified name.

    You can think of:

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

    as:

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

    To access the BaseClass01.x, use the base keyword:

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

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might seem to be a silly question at first, but please read on.
Okay, this might be a very silly beginner question, but: I've got an ClassA,
This might be a bit of a silly question but; If I have two
This might be a silly question but I am still learning. I have read
This might be a silly question but it struck me, and here i ask.
This might seem like a silly question but valgrind doesn't by default give you
This might sound like a silly question but is there a manual or a
HI, this might sound like a silly question but.. i've created several types in
This might be a silly question, but... I've been writing a number of classes
This might be a silly question but I couldn't find a good answer in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.