Hi Guys
NP70 readonly Keyword
http://www.java2s.com/Code/CSharp/Language-Basics/Demonstratetheuseofreadonlyvariables.htm
I got this program from the above website. Reserved Keyword readonly has been used in the program. Program is producing similar output without readonly Keyword. I wish to know in this circumstance what is the purpose of using readonly Keyword.
Anyone knows please explain the reason.
Thank you
namespace nsReadOnly
{
using System;
public class ReadOnly
{
static double DegreeFactor = 1;
static double MilFactor = 0.05625;
static public void
{
double degrees = 42;
// 1 degree = 17.77778 mils
double mils = degrees * 17.77778;
// 1 degree = 0.017453 radians
double radians = degrees * 0.017453;
clsArea InDegrees = new clsArea(DegreeFactor);
InDegrees.Angle = degrees;
InDegrees.Radius = 50;
Console.WriteLine("Area of circle is {0,0:F1}", InDegrees.Area);
// Radians are the default, so you can use the parameterless
// constructor
clsArea InRadians = new clsArea();
InRadians.Angle = radians;
InRadians.Radius = 50;
Console.WriteLine("Area of circle is {0,0:F1}", InRadians.Area);
clsArea InMils = new clsArea(MilFactor);
InMils.Angle = mils;
InMils.Radius = 50;
Console.WriteLine("Area of circle is {0,0:F1}", InMils.Area);
}
}
class clsArea
{
private const double pi = 3.14159;
private const double radian = 57.29578;
private readonly double m_Factor = 1;
private double m_Angle;
private double m_Radius;
public clsArea()
{
}
public clsArea(double factor)
{
m_Factor = factor / 57.29578;
}
public double Angle
{
get { return (m_Angle); }
set { m_Angle = value; }
}
public double Radius
{
get { return (m_Radius); }
set { m_Radius = value; }
}
public double Area
{
get
{
return (m_Radius * m_Radius * pi * m_Angle * m_Factor / (2 * pi));
}
}
}
}
/*
Area of circle is 916.3
Area of circle is 916.3
Area of circle is 916.3
Press any key to continue . . .
*/
Posted Dec 16, 2007, 2:19 PM
Thank you, Alan.
AlanPosted Dec 16, 2007, 1:51 PM
The significance of a readonly field can only be seen if you attempt to set it to a new value other than in its class's constructor.
For example, if you place this line in the Main() method, an exception will be thrown:
p1.y = 26;
If 'y' hadn't been declared readonly, then there would have been no exception.
So, readonly is a preventative language feature rather than a necessary one.
Posted Dec 16, 2007, 1:40 PM
Program in the above website is producing similar result without readonly. Can anyone explain the significance of the readonly keyword in this program.
// cs_readonly_keyword.cs
// Readonly fields
using System;
public class ReadOnlyTest
{
class SampleClass
{
public int x;
// Initialize a readonly field
public readonly int y = 25;
public readonly int z;
public SampleClass()
{
// Initialize a readonly instance field
z = 24;
}
public SampleClass(int p1, int p2, int p3)
{
x = p1;
y = p2;
z = p3;
}
}
static voidMain ()
{
SampleClass p1 = new SampleClass(11, 21, 32); // OK
Console.WriteLine("p1: x={0}, y={1}, z={2}", p1.x, p1.y, p1.z);
SampleClass p2 = new SampleClass();
p2.x = 55; // OK
Console.WriteLine("p2: x={0}, y={1}, z={2}", p2.x, p2.y, p2.z);
}
}
/*
p1: x=11, y=21, z=32
p2: x=55, y=25, z=24
Press any key to continue . . .
*/
Posted Dec 15, 2007, 6:41 PM
Thank you for your help, Alan.
AlanPosted Dec 15, 2007, 5:44 PM
There's a full program example of readonly in the MSDN online docs:
http://msdn2.microsoft.com/en-us/library/acdd6hb7(VS.90).aspx
In my experience, declaring an instance field as readonly is not done very often as most programmers prefer to use properties rather than expose fields directly and you can make a property readonly by just declaring a 'get'.
However, static readonly fields are used a fair bit because it's the only way you can declare a constant which cannot be evaluated until runtime and I thought Brandon's example brought out the difference between 'const' and 'readonly' very well.
Posted Dec 15, 2007, 11:26 AM
Posted Dec 15, 2007, 10:02 AM
public class MyClass
{
private int myNum;
public MyClass(int myNum)
{
this.myNum = myNum;
}
public int MyNum
{
get { return this.myNum; }
}
}
public class ReadOnly
{
//The "new" keyword prevents this
private const MyClass myClass = new MyClass(12);
//So declare myClass as read only
private readonly MyClass myClass = new MyClass(12);
//Method to try to reassign to myClass
public void ReassignMyClass()
{
//This will generate an error
myClass = new MyClass(22);
//But you can still access myClass's members
int outNum = myClass.MyNum;
}
}
Posted Dec 15, 2007, 9:37 AM
It is much appreciated if you support by an example, either you can modify this program or give simple example where readonly is necessary.
AlanPosted Dec 15, 2007, 8:40 AM
The 'readonly' modifier is used when you want a field to always have the same value for a given instance. It can only be set at the same time it is declared or in the constructor.
In the example you gave it made no difference whether m_Factor was declared as readonly or not, because no attempt was made to set it again after it was declared and then in the constructor. If an attempt had been made to set it in another method, then a compile time error would have occurred.
It's also possible to have the 'static readonly' combination where you want a field to always have the same value for all instances. This can be used where you want to create a constant but can't use the 'const' modifier because the latter can only be set using a literal (eg: 1, 2.3, 'a', "abc") or other constant expression. For example:
public static readonly Color Blue = new Color(0, 0, 255);
Here, 'Blue' can't be declared as a const becuase of the presence of the 'new' expression.
The essential difference is that the value of a 'const' is determined at compile time but the value of a 'static readonly' field is determined at runtime.