Hey guys I am brand new to c#. I was reading this book and this fill in the blanks says __ are the data fields or local variables whose values cannot be modifed? Can someone tell me the answer? thanks. ps: total newbie I searched youtube but I was messed up in code. thanks.
-
Constants can be the answer. – KKS Sep 23 '13 at 16:15
-
@Yoda only for primitive types though – undefined Sep 23 '13 at 16:16
-
2@Yoda Not if your search engine is Youtube – Ralf Sep 23 '13 at 16:19
-
1`readonly` is also the correct answer. Since the question is stated as "are the data fields **or** local variables". Readonly can't be used for local variables, but can be used for fields, so *or* condition isn't violated. The difference between const and readonly is that you can assign readonly fields in ctor. They are not compile time constants, while const are. – Ilya Ivanov Sep 23 '13 at 16:19
2 Answers
The complete answer is twofold:
1) For primitive types (such as int
, double
, string
, etc.) a const
modifier can be used, which indeed means that the value of the variable of this type cannot be changed by any means
2) For non-primitive types a readonly
modifier exists. Note, that you cannot apply const
to non-primitive types. It means the same thing for reference and value types: the
reference to this variable cannot be changed after the construction of this object.
However, it has different consequences:
2.1) The readonly
object itself can be changed by means of it's public API. For example:
class Foo{
private readonly List<int> list;
public Foo(){ list = new List<int>();}
public Test()
{
list = new List<int>(); // invalid; your reference is readonly
list.add(5);//works, you are changing the object, but not touching it's reference
}
}
2.2) For value types readonly
one must be careful while using readonly
modifier - it may lead to subtle errors with mutable structs. See C#: Why do mutations on readonly structs not break? for example. However, if your struct
is immutable you effectively get the semantics of const
keyword
-
`readonly` can be used for non-primitive types. Both keywords work the same way for values and reference types and their semantics doesn't change depending of the type of a variable. – Ilya Ivanov Sep 23 '13 at 16:23
-
the consequenses for programmer do change. You don't have problems with mutable classes, but you do have them with mutable structs – undefined Sep 23 '13 at 16:25
-
`You don't have problems with mutable classes` depends on your intention. If I mark reference variable with readonly I may suppose it won't be changed, but if an object is mutable of that type - I might be very wrong. – Ilya Ivanov Sep 23 '13 at 16:27
-
@IlyaIvanov I am mentioning this in my answer. The thing is that readonly != const by any means, which I tried to emphasise – undefined Sep 23 '13 at 16:30
-
totally agree. From my side I just wanted to note, that `readonly` behaves exactly the same for value and reference types. As well as `const` behaviour doesn't change for value and reference types. – Ilya Ivanov Sep 23 '13 at 16:32