0

I am writing following code,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReadOnlyObject
{
    class Program
    {
        private readonly int a = 20;
        private readonly int b;
        public int propa{get;private set;}

        public int propb { get; private set; }

        public Program(int tmp)
        {
                   b = tmp;
        }
        static void Main(string[] args)
        {
                   Program obj1 = new Program(30);

                   Console.WriteLine(obj1.propa);        // Console.WriteLine(obj1.a);

                   Console.WriteLine(obj1.propb);        // Console.WriteLine(obj1.b);

                   Console.Read();
        }
    }
}

After executing the above i got o/p as follows,

0
0

And when I change the code by replacing two commented statements printing direct member variables I got output as,

20
30

Why is so? As far I know about properties they are associated with their definition in order the member variables are declared.

5 Answers 5

3

You have confusion about auto property, so:

private readonly int a = 20;
private readonly int b;
public int propa{get {return a; }}

public int propb { get {return b;} private set {b = value;} }

now this will print 20, 30

Sign up to request clarification or add additional context in comments.

1 Comment

K so,, " properties are associated with their defination in order the member variables are declared." is not true at all.
1

There are two ways to define properties in C#.

The first, the traditional way;

int myProperty;

public int MyProperty 
{
  get { return myProperty; }
  set { myProperty = value; }
}

the second, the auto-property;

   public int MyProperty {get;set;}

The first contains a backing variable that you reference in the property accessor. The second implicitly creates a backing variable, because the developers of the language understood that there are a lot of cases where you just need a property!

You can put scope on the auto-property, because you might want to prevent people from setting the value, but internally to the object you should be able to update the value of it.

"As far I know about properties they are associated with their defination in order the member variables are declated."

Just to clarify all of what you were asking, unless I am reading this statement incorrectly, you're thinking that if you declare variable a and b and the property a and property b that they'll be associated. This is an incorrect assumption.

4 Comments

so in case of setting properties of two int member variable how can I achieve using auto property?
Code isn't going to work here. It depends on your usage, if they're to hold values that are freely available, then make them both as per my auto property example.
yep I got the concept! So if I say I have auto implemented property it means I am declaring another member variable which can hold it's own value. Auto property is not going to assign the value to other member variable. But in case of implemented property I can do so.
Correct.Auto-Properties do still create private member vars behind the scenes,just the implementation is hidden from you because you don't care.You just want a value to be held for you and accessed via the property.
0

propa and a are not associated in your example. The compiler is making them auto-implemented properties. http://msdn.microsoft.com/en-us/library/bb384054.aspx If you want propa associated with a then you'd do:

public int propa{get { return a;} }

Comments

0

You're not initializing either property. You also can't set the value of b from a setter if it's marked readonly. You can implement your own 'readonly' type by just not letting the value get set more than once. (Although it doesn't stay true to the constraint that it needs to be initialized in the constructor)

Try this:

private readonly int a = 20;
    public int A { get { return a; } }

    private int b;
    private bool bInitialized = false;

    public int B
    {
        get { return b; }
        private set
        {
            if (bInitialized) return;

            bInitialized = true;
            b = value;
        }
    }

Comments

0

The way your code is written propb and propa CANNOT be set outside the scope of the class. so remove the keyword private from the set keyword

if you wrote this.propb = b in your constructor, then I think it should work more like you are expecting.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.