include::../why.adoc[]

include::../how.adoc[]

=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
public bool Seek(int[] input)
{
  var target = 32;  // Noncompliant
  foreach (int i in input)
  {
    if (i == target) 
    {
      return true;
    }
  }
  return false;
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
public bool Seek(int[] input)
{
  const int target = 32;
  foreach (int i in input)
  {
    if (i == target)
    {
      return true;
    }
  }
  return false;
}
----

==== Noncompliant code example

[source,csharp,diff-id=2,diff-type=noncompliant]
----
public class Sample
{
  public void Method()
  {
    var context = $"{nameof(Sample)}.{nameof(Method)}";  // Noncompliant (C# 10 and above only)
  }
}
----

==== Compliant solution

[source,csharp,diff-id=2,diff-type=compliant]
----
public class Sample
{
  public void Method()
  {
    const string context = $"{nameof(Sample)}.{nameof(Method)}";
  }
}
----

== Resources

=== Documentation

* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const[const]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Add the 'const' modifier to 'xxx'.


endif::env-github,rspecator-view[]
