From 9b4ef0d992c789f2375697001f1b4941b9ec53a6 Mon Sep 17 00:00:00 2001 From: "Meaghan Osagie (Lewis)" Date: Thu, 4 Sep 2025 13:54:19 -0700 Subject: [PATCH 1/2] update example --- docs/csharp/misc/cs0131.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/misc/cs0131.md b/docs/csharp/misc/cs0131.md index 4cfff15eb88a9..c54e0559f63fe 100644 --- a/docs/csharp/misc/cs0131.md +++ b/docs/csharp/misc/cs0131.md @@ -24,7 +24,7 @@ The left-hand side of an assignment must be a variable, property or indexer // CS0131.cs public class MyClass { - public int i = 0; + public const int i = 0; public void MyMethod() { i++ = 1; // CS0131 From 55144aa5456069e85a99e6b0a6f4f9477542624b Mon Sep 17 00:00:00 2001 From: "Meaghan Osagie (Lewis)" Date: Thu, 4 Sep 2025 14:56:18 -0700 Subject: [PATCH 2/2] add a new example --- docs/csharp/misc/cs0131.md | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/docs/csharp/misc/cs0131.md b/docs/csharp/misc/cs0131.md index c54e0559f63fe..3875c74c54325 100644 --- a/docs/csharp/misc/cs0131.md +++ b/docs/csharp/misc/cs0131.md @@ -24,7 +24,7 @@ The left-hand side of an assignment must be a variable, property or indexer // CS0131.cs public class MyClass { - public const int i = 0; + public int i = 0; public void MyMethod() { i++ = 1; // CS0131 @@ -33,14 +33,34 @@ public class MyClass } public static void Main() { } } -``` +``` + +## Example 2 + + The following sample generates CS0131 when assigning to a constant field. -## Example 2 +```csharp +// CS0131b.cs +public class B +{ + public static int Main() + { + const int j = 0; + j = 1; // CS0131 + // try the following lines instead + // int j = 0; + // j = 1; + return j; + } +} +``` + +## Example 3 This error can also occur if you attempt to perform arithmetic operations on the left hand side of an assignment operator, as in the following example. ```csharp -// CS0131b.cs +// CS0131c.cs public class C { public static int Main()