0

i am a newbie to c#. i was reading about properties. i have coded this and i think the result should be displayed "Value to big" but it's not showing anything. please tell me where am i wrong. thanks.

       private int _age;

        public int Age
        {
            get { return _age; }
            set
            {
                if (value < 10)
                {
                    Age = value;
                    Console.WriteLine("Value to Small");
                }
                else
                {
                    Console.WriteLine("Value to Big");
                }
            }
        }

        private static void Main(string[] args)
        {
            var banmeet = new Program();
            banmeet._age = 22;
            Console.ReadLine();
        }
    }
}
Cloudboy22
  • 1,496
  • 5
  • 21
  • 39
  • 1
    You should read about private backing fields and public properties [here](http://msdn.microsoft.com/en-us/library/ms173118.aspx) – Harrison Nov 04 '13 at 05:13

4 Answers4

3

You need to set Age instead of _age. _age is simply a private member, where as Age is the property you are wanting to set.

banmeet.Age = 22;

Also as Tim Jarvis pointed out, inside of the age setter, you need to set the age to _age.

if (value < 10)
{
    _age = value;
    Console.WriteLine("Value to Small");
}

You can read more about properties vs fields here.

Community
  • 1
  • 1
matth
  • 6,112
  • 4
  • 37
  • 43
  • 2
    plus, in the setter, you should set _age = value (not Age) – Tim Jarvis Nov 04 '13 at 05:03
  • Thanks but please correct me if i am wrong but aren't the values going inside the private variable or they are set in property? – Cloudboy22 Nov 04 '13 at 05:04
  • When you create a new object, `var banmeet = new Class();`, you won't have access to `_age` since it's private. When you create the `Age` property, you have to return and set the `_age` variable, since `Age` itself doesn't contain a value. – matth Nov 04 '13 at 05:13
1

You have some mistakes. First you want use this code:

    private int _age;

    public int Age
    {
        get { return _age; }
        set
        {
            if (_age < 10)
            {
                _age = value;
                Console.WriteLine("Value to Small");
            }
            else
            {
                Console.WriteLine("Value to Big");
            }
        }
    }

    private static void Main(string[] args)
    {
        var banmeet = new Program();
        banmeet.Age = 22;
        Console.ReadLine();
    }

Then if use above code output is Value to Small because _age is 0.

For the correct result you should use this:

   private int _age;

        public int Age
        {
            get { return _age; }
            set
            {
                _age = value;

                Console.WriteLine(_age < 10 ? "Value to Small" : "Value to Big");
            }
        }

        private static void Main(string[] args)
        {
            var banmeet = new Program();
            banmeet.Age = 22;
            Console.ReadLine();
        }
Harrison
  • 3,843
  • 7
  • 22
  • 49
0

You're setting _age, which is a field, not a property. So your property setter won't be called.

Also, you shouldn't set Age in your property setter, you should set _age. Setting Age will result in infinite recursion (and ultimately a stack overflow) since the setter is invoking itself.

Kyle
  • 6,500
  • 2
  • 31
  • 41
0

Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details. Better to set the value for _age instead of Age .

    public int Age
    {
        get { return _age; }
        set
        {
            if (value < 10)
            {
                _age = value;
                Console.WriteLine("Value to Small");
            }
            else
            {
                Console.WriteLine("Value to Big");
            }
        }
    }
Thilina H
  • 5,754
  • 6
  • 26
  • 56