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 8541213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:46:50+00:00 2026-06-11T11:46:50+00:00

I have a class ExProperty something like below: class ExProperty { private int m_asimplevar;

  • 0

I have a class ExProperty something like below:

class ExProperty
{
    private int m_asimplevar;
    private readonly int _s=2;

    public ExProperty(int iTemp)
    {
        m_asimplevar = iTemp;  
    }

    public void Asimplemethod()
    {
        System.Console.WriteLine(m_asimplevar);
    }

    public int Property
    {
        get {return m_asimplevar ;}
        //since there is no set, this property is just made readonly.
    }
}

class Program
{
    static void Main(string[] args)
    {
        var ap = new ExProperty(2);
        Console.WriteLine(ap.Property);
    }
}
  1. What is the sole purpose of making/using a property readonly or write only? I see, through the following program that readonly achieves the same purpose!

  2. When I make the property read-only, I think it should not be writable. When I use

    public void Asimplemethod()
    {
        _s=3; //Compiler reports as "Read only field cannot be used as assignment"
        System.Console.WriteLine(m_asimplevar);
    }
    

    Yes, this is ok.

    But, If i use

    public ExProperty(int iTemp)
    {
        _s = 3 ; //compiler reports no error. May be read-only does not apply to constructors functions ?
    }
    

    Why does the compiler report no error in this case ?

  3. Is declaring _s=3 ok? Or should I declare _s and assign its value using a constructor?

  • 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-06-11T11:46:51+00:00Added an answer on June 11, 2026 at 11:46 am

    Yes, the readonly keyword means that the field can be written to only in a field initializer and in constructors.

    If you want, you can combine readonly with the property approach. The private backing field for the property can be declared readonly while the property itself has only a getter. Then the backing field can be assigned to only in constructors (and in its possible field initializer).

    Another thing you could consider is making a public readonly field. Since the field itself is read-only, you actually don’t achieve much from the getter if all it does is returning the field value.

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

Sidebar

Related Questions

I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have: class first{ private: int *array; public: first(int x){ array = new int[x][10];
I have class with back reference: public class Employee : Entity { private string
I have class like this below shown. which contains the shopping items where the
I have class that looks like this: class A { public: class variables_map vm
I have class Person: public class Person : INotifyPropertyChanged { private String name =
I have: class BASE{ public: virtual void func1() = 0; }; Then I have
I have class implementing Runnable: public class Abc implements Runnable{ public synchronized void run(){
I have class where the relevant part looks like class C { void Method<T>(SomeClass<T>
I have class (MyCustomWebView) that extend webview can i do something like this ?

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.