surapong

Can somebody explain this code to me why it compiles

Code Snippet

class Foo

{

public int X = 4;

}

class TestReadOnly

{

public readonly Foo A = new Foo();

public void ChangeReadOnly() { A.X *= 2; }

}

class Program

{

static void Main(string[] args)

{

TestReadOnly tmp = new TestReadOnly();

Console.WriteLine("tmp.A.X : {0}", tmp.A.X);

tmp.ChangeReadOnly();

Console.WriteLine("tmp.A.X : {0}", tmp.A.X);

}

}

I thought the readonly value can't be changed..

Here is my result:

Code Snippet

tmp.A.X : 4
tmp.A.X : 8
Press any key to continue . . .



Re: Visual C# Language readonly question

Tom Meschter MSFT

"readonly" applies to the field, not to the object it refers to. The following will not work:

Code Snippet

class TestReadOnly

{

public readonly Foo A = new Foo();

public void ChangeReadOnly() { A = new Foo(); } // this is an error

}

However, in the sample code you provided, you're not trying to change A; you're changing a field of A. That field would need to have the readonly keyword in order to prevent your ChangeReadOnly from working.

-Tom Meschter
Software Dev, Visual C# IDE






Re: Visual C# Language readonly question

surapong

Wow, that's deep.. I have misunderstood the readonly all along..

Thanks Tom for clearing this thing up for me..

Well, next question.. Is there any way to enforce A (after declare with modifier readonly or whatever) not to be changed at all even with its field ... Like in C++, I can use const to do the job:

Code Snippet

class Foo

{

public:

int X;

Foo() : X(4) {}

};

void main()

{

const Foo* tmp = new Foo();

tmp->X *= 2; // will give me compile-error

}

But in C#, I am not allowed to use const for run-time instantiation...

Any idea

surapong l.





Re: Visual C# Language readonly question

anubisascends

const is used to declare a constant data type, for instance:

Code Snippet

const double Pi = 3.14;

Since Pi will never change (for the purposes of our program), we want to declare it as a const.






Re: Visual C# Language readonly question

surapong

But const cannot be use for run-time instantiation..

That is, you CANNOT do

const Foo a = new Foo();





Re: Visual C# Language readonly question

Nikhil Malhotra

Just to add to what Tom mentioned, the following code would give you a compile time error

namespace Readonlytest

{

class Foo

{

public readonly int X = 4;

}

class TestReadOnly

{

public Foo A = new Foo();

public void ChangeReadOnly() { A.X *= 2; }

}

class Program

{

static void Main(string[] args)

{

TestReadOnly tmp = new TestReadOnly();

Console.WriteLine("tmp.A.X : {0}", tmp.A.X);

tmp.ChangeReadOnly();

Console.WriteLine("tmp.A.X : {0}", tmp.A.X);

}

}

}

As far as the question about constants and readonly fields. The constants are compile time constants and readonly fields are run time constants. To assign a readonly field it has to be done either in a constrcutor or during declaration

for e.g.

public readonly int x=4;






Re: Visual C# Language readonly question

tlehr

Readonly protects A member from change. Since A is just a reference because Foo is a reference type, the only thing that can't be changed is that reference. Solution, get rid of the reference by changing Foo to a struct. Now its members will be protected from writes because A is contains a value and not a reference.