Can you pass a and store a reference in primary constructor? #7740
-
After learning about primary constructors, there's a question about whether they should be mutable or not. If mutable can they also be references? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@Joebeazelman It's not very clear waht you're asking. Can you give an example?
Constructors are not themselves mutable. They are just a way to create and initialize a value of a type. A code sample may clarify what you're asking. Thanks! |
Beta Was this translation helpful? Give feedback.
-
If the question is whether the primary constructor parameters can be reference types and if they can be mutated, the answer is yes, on both counts: public class C(List<string> strings) {
public void Add(string value) => strings.Add(value);
public void Reset() => strings = new List<string>();
} |
Beta Was this translation helpful? Give feedback.
If the question is whether the primary constructor parameters can be reference types and if they can be mutated, the answer is yes, on both counts: