Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6146941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:01:57+00:00 2026-05-23T19:01:57+00:00

I am writing following code, using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:01:58+00:00Added an answer on May 23, 2026 at 7:01 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got tired of writing the following code: /* Commenting out irrelevant parts public
I'm writing an XML file with the following code: Source source = new DOMSource(rootElement);
Mono won't fire the following code: I get internal server error 500, error writing
I have the following section of code in an app that I am writing:
I am writing code to performance test a web site. I have the following
I am writing the following in LINQ Enumerable.Range(50, 100).Select(n => n/10 == 1) but
I have this following code for bringing page attachments to the user: private void
i created a text file list.txt in commonapplicationdatafolder by using the following VBscript.i am
I'm drawing an image and outputing it to web page using the following code:
I'm writing item template for repeater in separate control, and then I'm using following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.