2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-07-03 18:22:04 +02:00
While the properties of a https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly[`readonly`] https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types[reference type] field can still be changed after initialization, those of a `readonly` https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types[value type] field, such as a https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct[`struct`], cannot.
2021-04-28 16:49:39 +02:00
2023-07-03 18:22:04 +02:00
If the member could be either a `class` or a `struct` then assignment to its properties could be unreliable, working sometimes but not others.
2021-04-28 16:49:39 +02:00
2023-07-03 18:22:04 +02:00
== How to fix it
2021-04-28 16:49:39 +02:00
2023-07-03 18:22:04 +02:00
There are two ways to fix this issue:
2021-04-28 18:08:03 +02:00
2023-07-03 18:22:04 +02:00
* Restrict the type of the field to a `class`
* Remove the assignment entirely, if it is not possible to restrict the type of the field
2021-04-28 16:49:39 +02:00
2023-07-03 18:22:04 +02:00
=== Code examples
2021-04-28 16:49:39 +02:00
2023-07-03 18:22:04 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-07-03 18:22:04 +02:00
[source,csharp,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
interface IPoint
{
2023-07-03 18:22:04 +02:00
int X { get; set; }
int Y { get; set; }
2021-04-28 16:49:39 +02:00
}
2023-07-03 18:22:04 +02:00
class PointManager<T1, T2>
where T1 : IPoint
where T2 : IPoint
2021-04-28 16:49:39 +02:00
{
2023-07-03 18:22:04 +02:00
readonly T1 point1; // this could be a struct
readonly T2 point2; // this could be a struct
public PointManager(T1 point1, T2 point2)
{
this.point1 = point1;
this.point2 = point2;
}
public void MovePoints(int newX)
{
point1.X = newX; //Noncompliant: if point is a struct, then nothing happened
point2.X = newX; //Noncompliant: if point is a struct, then nothing happened
}
2021-04-28 16:49:39 +02:00
}
----
2023-07-03 18:22:04 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-07-03 18:22:04 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
interface IPoint
{
2023-07-03 18:22:04 +02:00
int X { get; set; }
int Y { get; set; }
2021-04-28 16:49:39 +02:00
}
2023-07-03 18:22:04 +02:00
class PointManager<T1, T2>
where T1 : IPoint
where T2 : class, IPoint
2021-04-28 16:49:39 +02:00
{
2023-07-03 18:22:04 +02:00
readonly T1 point1; // this could be a struct
readonly T2 point2; // this is a class
public PointManager(T1 point1, T2 point2)
{
this.point1 = point1;
this.point2 = point2;
}
public void MovePoints(int newX) // assignment to point1 has been removed
{
point2.X = newX; // Compliant: point2 is a class
}
2021-04-28 16:49:39 +02:00
}
----
2021-04-28 18:08:03 +02:00
2023-07-03 18:22:04 +02:00
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly[readonly (C# Reference)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types[Reference types (C# reference)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types[Value types (C# reference)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct[Structure types (C# reference)]
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2023-07-03 18:22:04 +02:00
ifdef::env-github,rspecator-view,env-vscode[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Restrict "x" to be a reference type or remove this assignment of "y"; it is useless if "x" is a value type.
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 13 May 2015, 20:13:12 Ann Campbell wrote:
\[~tamas.vajk]
* I've got the name of the equivalent R# rule, but I'm not sure how to find its key...
* I suspect I may have missed some of the subtleties intended in this rule. Feel free to start an edit iteration
=== on 18 May 2015, 09:15:33 Tamas Vajk wrote:
Updated the rule. Could you please check its language, and if it makes sense to you too?
=== on 21 May 2015, 14:34:22 Ann Campbell wrote:
see what you think [~tamas.vajk]
=== on 22 May 2015, 11:28:16 Tamas Vajk wrote:
Looks good, I adjusted some wording in the first sentence of the description
=== on 22 May 2015, 12:10:14 Ann Campbell wrote:
Thanks [~tamas.vajk]. Looks good
2023-07-03 18:22:04 +02:00
endif::env-github,rspecator-view,env-vscode[]