Click here to Skip to main content
14,930,017 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: Naming stuff... Pin
honey the codewitch12-Aug-19 19:43
mvahoney the codewitch12-Aug-19 19:43 
GeneralRe: Naming stuff... Pin
Sander Rossel12-Aug-19 21:55
professionalSander Rossel12-Aug-19 21:55 
GeneralRe: Naming stuff... Pin
honey the codewitch12-Aug-19 21:58
mvahoney the codewitch12-Aug-19 21:58 
GeneralRe: Naming stuff... Pin
Sander Rossel12-Aug-19 22:28
professionalSander Rossel12-Aug-19 22:28 
GeneralRe: Naming stuff... Pin
honey the codewitch12-Aug-19 22:58
mvahoney the codewitch12-Aug-19 22:58 
GeneralRe: Naming stuff... Pin
Sander Rossel12-Aug-19 23:06
professionalSander Rossel12-Aug-19 23:06 
GeneralRe: Naming stuff... Pin
honey the codewitch12-Aug-19 23:36
mvahoney the codewitch12-Aug-19 23:36 
GeneralRe: Naming stuff... Pin
Sander Rossel13-Aug-19 0:19
professionalSander Rossel13-Aug-19 0:19 
Interesting.
With private fields it's no a problem at all.
The base field simply isn't accessible from the derived class.
The two fields are simply "BaseClass.someField" and "DerivedClass.someField".
Things get different when you make the base class field public.
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new Derived().SomeValue);
        Console.WriteLine(new Derived().BaseSomeValue);
    }
}

public class Whatever
{
    public string someValue;
}

public class Derived : Whatever
{
    private string someValue;

    public Derived()
    {
        base.someValue = "Base someValue.";
        someValue = "Witch someValue?"; // using 'this' does nothing.
    }

    public string SomeValue => someValue;
    public string BaseSomeValue => base.someValue;
}
This prints "Witch someValue?" and "Base someValue.", pretty much as you'd expect.
Visual Studio only gives me a warning that someValue hides an inherited base member and that I should use the new keyword if hiding was intended.
Basically, both are treated as separate variables, and settings someValue will not set someValue in the base class, neither will setting base.someValue do anything for someValue.
Adding the new keyword gets rid of the warning, but doesn't seem to change anything.

To get back to your specific example, same thing.
new Derived().foo will point to the property, new Base().foo will point to the field (assuming you meant it to be public).
And you get a warning that foo hides an inherited member so you should add the new keyword.

All in all it's pretty confusing and best avoided Smile | :)

GeneralRe: Naming stuff... Pin
sasadler13-Aug-19 5:49
Membersasadler13-Aug-19 5:49 
GeneralRe: Naming stuff... Pin
honey the codewitch12-Aug-19 19:40
mvahoney the codewitch12-Aug-19 19:40 
GeneralRe: Naming stuff... Pin
Member 916705712-Aug-19 21:15
MemberMember 916705712-Aug-19 21:15 
GeneralRe: Naming stuff... Pin
Sander Rossel12-Aug-19 22:04
professionalSander Rossel12-Aug-19 22:04 
GeneralRe: Naming stuff... Pin
Member 916705712-Aug-19 22:15
MemberMember 916705712-Aug-19 22:15 
GeneralRe: Naming stuff... Pin
Sander Rossel12-Aug-19 22:47
professionalSander Rossel12-Aug-19 22:47 
GeneralRe: Naming stuff... Pin
Member 916705712-Aug-19 23:23
MemberMember 916705712-Aug-19 23:23 
GeneralRe: Naming stuff... Pin
Sander Rossel13-Aug-19 0:02
professionalSander Rossel13-Aug-19 0:02 
GeneralRe: Naming stuff... Pin
Member 916705713-Aug-19 0:34
MemberMember 916705713-Aug-19 0:34 
GeneralRe: Naming stuff... Pin
Sander Rossel13-Aug-19 10:26
professionalSander Rossel13-Aug-19 10:26 
GeneralRe: Naming stuff... Pin
Member 916705714-Aug-19 3:18
MemberMember 916705714-Aug-19 3:18 
GeneralRe: Naming stuff... Pin
Sander Rossel14-Aug-19 3:26
professionalSander Rossel14-Aug-19 3:26 
GeneralRe: Naming stuff... Pin
Member 916705714-Aug-19 3:30
MemberMember 916705714-Aug-19 3:30 
GeneralRe: Naming stuff... Pin
Sander Rossel14-Aug-19 4:23
professionalSander Rossel14-Aug-19 4:23 
GeneralRe: Naming stuff... Pin
stevetabler13-Aug-19 5:51
Memberstevetabler13-Aug-19 5:51 
GeneralRe: Naming stuff... Pin
Abbas A. Ali15-Aug-19 20:52
professionalAbbas A. Ali15-Aug-19 20:52 
GeneralRe: Naming stuff... Pin
Sander Rossel16-Aug-19 1:41
professionalSander Rossel16-Aug-19 1:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.