Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is the extract of the code:
using System;

<pre>
namespace ConsoleApp2
{
    class Program
    {


        class A
        {
            int Fa = 1 ;

            public A() { ++Fa; }

            public int f { get { return ++Fa; } set { Fa += value; } }
        }
        class B : A
        {
            static int Fb = 2 ;
            public B() { Fb++; base.f = 2; }
            public new int f { get{ return Fb++ ;}  set { Fb += value ;} }
                public override string ToString() { return String.Format("{0}", base.f + f); }
        }
            static void Main()
            {
            B obj1 = new B(), obj2 = new B(), obj3 = new B();
            obj3.f=obj2.f ;
            Console.Write(obj1);
            Console.Write(obj1);
            Console.Write(obj1);
            }
        
    }
}


I can't work out what happens in line obj3.f=obj2.f

What I have tried:

I supposed that obj2.f calls the get of the property f, then the set is executed and aftter that the value of the set becomes the value in obj3.f, but that doesn't live up to the correct answer.
Posted
Updated 17-Jan-20 1:48am
v2
Comments
phil.o 17-Jan-20 7:19am    
A getter which modifies the value it is supposed to return have a strong smell of design flaw.
Richard Deeming 17-Jan-20 7:45am    
Perhaps your confusion stems from the fact they you're writing obj1 to the console three times? I suspect you mean to write all three objects instead.

But as Phil said, a property get which modifies the value is a "code smell".
Member 14719149 17-Jan-20 8:23am    
Well, it's just a test task i recieved once and it surely is "smelly" and doesn't have any practical use, but the code is written here the way its author intended.

Basically, what you have done is a crime against nature, and you shouldn't be doing it at all. Property getters should never alter values; when they do things get very, very strange, particularly when static variables are involved, and the code becomes extremely hard to debug because the debugger uses the getters to fetch the values it displays for you - so the values you see in the debugger may or may not match what you expect, and your act of writing values to the console causes other values (including teh one you are displaying) to change.

Basically don't do it. Property getters should never change data, it is an extremely bad practice and leads to unpredictable results as you have seen.
 
Share this answer
 
Comments
Member 14719149 17-Jan-20 8:30am    
Yes, i can't but agree with you, this code is atrocious. The thing is, it's a test task i recieved not long ago, failed it, and now i'm trying to understand how it is supposed to work.
Put a breakpoint on the first line of the Main() method, then press F5 (assuming you are using Visual Studio) and start a debug session. From there you will be able to execute the code line by line, examining the values of your variables along the way.
Do you need help on how to conduct a debug session?
 
Share this answer
 
v2
Comments
OriginalGriff 17-Jan-20 7:50am    
That's not going to help; it'll make it worse - the debugger uses the property getter to display the values, so casually passing your mouse over your code during a breakpoint will alter the values it displays... Xo
ZurdoDev 17-Jan-20 8:36am    
If you step into the code of the getter it would help.
OriginalGriff 17-Jan-20 8:40am    
I wouldn't bank on it - it maye get even more confusing as the method you are stepping into will use the same method to display debug values ... You'd probably be better off using Debug.WriteLine and snapshotting values for review afterwards. Getters that modify values are a PITA to sort out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900