Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a field read only outside class

I have the following class (example):

public class Dog
{
    int numberOfTeeth;

    public Dog() 
    { 
        countTeeth(); 
    }

    private void countTeeth()
    {
        this.numberOfTeeth = 5; //this dog has seen better days, apparently
    }

}

After I create the dog object, it should have the number of teeth calculated. I'd like to be able to access that value without being able to modify it outside the class itself.

Dog d = new Dog();
int dogTeeth = d.numberOfTeeth; //this should be possible
d.numberOfTeeth = 10; //this should not

However, I can't figure out which access modifier will let me do this. I've tried all of the following:

If I make numberOfTeeth private, I cannot access it.
If I make numberOfTeeth protected internal, I can change this value outside the class.
If I make numberOfTeeth internal, I can change this value outside the class.
If I make numberOfTeeth protected, I cannot access it.
If I make numberOfTeeth public, I can change this value outside the class.

I also tried making it readonly but then was unable to set it outside the constructor.

Is there any access modifier which will allow me to do this? Or is there some other method of accomplishing this protection?

like image 727
Mansfield Avatar asked Nov 15 '12 15:11

Mansfield


People also ask

How to make a variable read-only in C#?

In C#, you can use a readonly keyword to declare a readonly variable. This readonly keyword shows that you can assign the variable only when you declare a variable or in a constructor of the same class in which it is declared.

What is read-only field in C#?

In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class. A readonly field can be assigned and reassigned multiple times within the field declaration and constructor.

How do I make my class readonly?

We can make a class read-only by making all of the data members private. Please note: If we make a class read-only, then we can't modify the properties or data members value of the class. If we make a class read-only, then we can only read the properties or data members value of the class.

How do I make a field read-only?

Sometimes making a field read-only can be useful, allowing you to then utilize that field for dynamic display of various messages. If you want to change a field to be read-only, you currently need to use JavaScript to do so. The following filter and function will help you accomplish this.

Is it possible to set readonly fields outside the Constructors?

It's a well know rule that the readonly fields can only be set in the static constructors or auto constructors. But if we just have a requirement to set it outside the constructors, can this really be done? The answer is yes, we can crack it by using Reflection.

What is the difference between const and readonly fields?

A readonly field can be assigned multiple times in the field declaration and in any constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example:

What is the use of readonly field in Java?

In a field declaration, readonlyindicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class. A readonly field can be assigned and reassigned multiple times within the field declaration and constructor.


1 Answers

Create a property with a private setter:

public int NumberOfTeeth
{
 get; private set;
}

Notice I changed it to Pascal Case to match most .NET style standards.

like image 174
D Stanley Avatar answered Oct 05 '22 07:10

D Stanley