Declaring readonly public property in C#

  • Thread starter Thread starter Jon Maz
  • Start date Start date
J

Jon Maz

Hi,

Quick & easy one - why does this code give an error?


=====
CODE
=====

private int numberOfPages;

public readonly int NumberOfPages
{
get { return numberOfPages; }
}


======
ERROR
======

The modifier 'readonly' is not valid for this item (pointing to line
"public readonly int NumberOfPages").


TIA,

JON
 
Jon Maz said:
Quick & easy one - why does this code give an error?

Because that's not how you make a property read-only. You make it read-
only by only giving it a get {...} accessor, not a set {...} accessor.
 
Hi Jon,

I thought of that, but it just looked kind of wrong (VB.Net habits, I
suppose), and the IDE gave me some kind of disapproving comment...

Still, I am now reassured.

Thanks!

JON
 
Back
Top