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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:42:13+00:00 2026-05-14T16:42:13+00:00

In a .NET application when should I use ReadOnly properties and when should I

  • 0

In a .NET application when should I use “ReadOnly” properties and when should I use just “Get”. What is the difference between these two.

private readonly double Fuel= 0;

public double FuelConsumption
{
    get
    {
        return Fuel;
    }
}        

or

private double Fuel= 0;

public double FuelConsumption
{
     get
     {
          return Fuel;
     }
}
  • 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-14T16:42:13+00:00Added an answer on May 14, 2026 at 4:42 pm

    Creating a property with only a getter makes your property read-only for any code that is outside the class.

    You can however change the value using methods provided by your class :

    public class FuelConsumption {
        private double fuel;
        public double Fuel
        {
            get { return this.fuel; }
        }
        public void FillFuelTank(double amount)
        {
            this.fuel += amount;
        }
    }
    
    public static void Main()
    {
        FuelConsumption f = new FuelConsumption();
    
        double a;
        a = f.Fuel; // Will work
        f.Fuel = a; // Does not compile
    
        f.FillFuelTank(10); // Value is changed from the method's code
    }
    

    Setting the private field of your class as readonly allows you to set the field value only in the constructor of the class (using an inline assignment or a defined constructor method).
    You will not be able to change it later.

    public class ReadOnlyFields {
        private readonly double a = 2.0;
        private readonly double b;
    
        public ReadOnlyFields()
        {
            this.b = 4.0;
        }
    }
    

    readonly class fields are often used for variables that are initialized during class construction, and will never be changed later on.

    In short, if you need to ensure your property value will never be changed from the outside, but you need to be able to change it from inside your class code, use a "Get-only" property.

    If you need to store a value which will never change once its initial value has been set, use a readonly field.

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For our application(desktop in .net), we want to have 2 databases in 2 different
I'm developing a Windows Forms Application in Visual Basic .NET with Visual Studio 2008.
In attempting to use the Performance Tools on an ASP.NET website I'm getting various
My question is as follows: I have a base controller (ASP.Net MVC controller) called
Context: .NET 3.5, VS2008. I'm not sure about the title of this question, so
I am trying to silent print PDF files from within a java application (more
I am fairly new to nHibernate and DDD, so please bear with me. I
does anybody know of a secure 'read-once' local file access system? Or how one
My manager has pointed out to me a few nifty things that one of

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.