readonly significance?

  • Thread starter Thread starter Jon Davis
  • Start date Start date
J

Jon Davis

In C#, what is the difference between a property block that has a readonly
keyword and a get block, versus a property block that has only a get block?

Thanks,
Jon
 
the difference is that the one with readonly won't compile. readonly can
only be set on the field, but not the property.
 
Now if you're asking whats the difference between Foo and Bar in this example

class Quux
{
public int readonly Foo;
private int bar;
public int Bar
{
get{ return bar;}
}
}

the difference is Foo can only be set by the Quux constructor code (or a field initializer). Bar can only be read by clients of Quux, but the value it delivers can be changed by Quux any time it sees fit.

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

The difference is that the one with just a get will compile ;-).

AFAIK readonly can only be applied to fields

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

In C#, what is the difference between a property block that has a readonly
keyword and a get block, versus a property block that has only a get block?

Thanks,
Jon


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.796 / Virus Database: 540 - Release Date: 13/11/2004



[microsoft.public.dotnet.languages.csharp]
 
That explains everything. Thank you very much.

Jon


Richard Blewett said:
Now if you're asking whats the difference between Foo and Bar in this example

class Quux
{
public int readonly Foo;
private int bar;
public int Bar
{
get{ return bar;}
}
}

the difference is Foo can only be set by the Quux constructor code (or a
field initializer). Bar can only be read by clients of Quux, but the value
it delivers can be changed by Quux any time it sees fit.
Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

The difference is that the one with just a get will compile ;-).

AFAIK readonly can only be applied to fields

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

In C#, what is the difference between a property block that has a readonly
keyword and a get block, versus a property block that has only a get block?

Thanks,
Jon


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.796 / Virus Database: 540 - Release Date: 13/11/2004



[microsoft.public.dotnet.languages.csharp]
 
Back
Top