0

I want to know that when you create an Automatic property and invoke the set in the main() method for a random value , where is that value being stored ?
as in this example :



    class Program
        {
            static void Main(string[] args)
            {
                Example W = new Example();

                W.Num = 10;
                Console.WriteLine("{0}", W.Num);

                Console.WriteLine("{0}", W.getNum());
            }
        }



    class Example
            {
                private int num;

                public int Num { get; set; }

                public int getNum() { return num; }
            }

why is the output :
10
0

5 Answers5

1

Because you are returning num, not Num. And num was not initialized, so this value is 0.

Jéf Bueno
  • 425
  • 1
  • 5
  • 23
  • so what's the benefit of using the automatic property !! isn't it used to initialize the data fields ?! –  Oct 17 '15 at 15:41
  • You're not initializing the field `num`. You have 2 different properties in class `Example`, `num` and `Num`. – Jéf Bueno Oct 17 '15 at 15:45
0

This is nothing abnormal here.

When you call

Example W = new Example();

then initially num = 0 and Num = 0;

you assigned Num, not num.

Dao Tuan Anh
  • 21
  • 1
  • 3
0

num in your Example class is redundant.

If you wrote this before automatic property initialisers were added to c#, it would look like this:

private int num;

 public int Num 
 { 
    get{ return num;}
    set{ num = value;}
}

Writing public public int Num { get; set; } is essentially the same thing behind the scenes. There is no need to implement getNum() (like Java), since this is equivalent to int a = w.Num;.

Leigh Shepperson
  • 1,043
  • 5
  • 13
  • so basically we can declare data fields as private and at the same time declare there "set" and "get" without having to declare a whole another property to accomplish this ? –  Oct 17 '15 at 15:53
  • You do not even have to include the data fields. Just writing the get set property is enough. The fields are added by the compiler so you don't have to worry about them. – Leigh Shepperson Oct 17 '15 at 15:55
0

Auto-implemented properties makes code cleaner when no additional logic is required for the getter or setter. The compiler actually generates a backing field for the auto-implemented property, but this backing field is not visible from your code.

In your example there is no connection between the num field and the Num property, so there no reason why the num should change.

Sam
  • 7,252
  • 16
  • 46
  • 65
Lukas Kabrt
  • 5,441
  • 4
  • 43
  • 58
0

if use new keyword , you created new instance your class And all object recreated.

For Example ;

    class Program
    {
        static void Main(string[] args)
        {
            Example W = new Example();

            W.Num = 10;

            Example W1 = new Example();

            Console.WriteLine("{0}", W.Num);  //10
            Console.WriteLine("{0}", W1.Num); //0
        }
    }

this is only information your answer ; you returning different variable. you not set them.