JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A static field has been modified from a non-static method.
Static fields can be updated from a static context, but this is a bad programming practice:
In object-oriented code, fields should mostly be instance fields; static fields should be used in rare situations, such as for constants. This checker looks for static fields that are assigned from non-static contexts. It only considers as acceptable assignments that are used for the lazy initialization of static fields, although they might occur in non-static contexts. That is current programming practice.
| Class Name | Static Field Assigned Non-Static (C#) | |||
|---|---|---|---|---|
| Significance | reliability | |||
| Mnemonic | CSHARP.CLASS.STATICMOD | |||
| Categories |
|
|||
| Availability | Available for C# only. |
|||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Static Field Assigned Non-Static (C#)" |
using System;
using System.Collections;
using System.Collections.Generic;
namespace DocumentationExamples
{
public class StaticFieldAccess1
{
private static int[] scores = new int[100];
private static int next;
public static void Main(string[] args)
{
new StaticFieldAccess1();
}
public void AddScore(int score)
{
if (next < scores.Length)
scores[next++] = score; // Two "Static Field Assigned Non-Static (C#)" warnings issued here:
// - one for 'next'
// - one for 'scores'
}
public float Average()
{
float sum = 0.0f;
for (int pos = 0; pos < next; pos++)
sum += scores[pos];
return sum / next;
}
}
public class StaticFieldAccess2
{
private static IDictionary session;
private static IDictionary session2;
public IDictionary GetSession()
{
if (session == null)
{
Console.WriteLine("Initializing session");
session = new Dictionary<string, string> (); // ok: correct lazy initialization idiom
}
return session;
}
public IDictionary GetSession2()
{
if (session == null) // The programmer probably intended to check session2 here
{
Console.WriteLine("Initializing session2");
session2 = new Dictionary<string, string> (); // "Static Field Assigned Non-Static (C#)" warning issued here
}
return session2;
}
}
}
In this example, the programmer should replace the static fields with instance fields:
private int[] scores = new int[100]; private int next;
Check if static fields can be replaced by instance fields or if methods can be made static.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.