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.0p0 Hot Tips | CONFIDENTIAL | CodeSecure Inc |
One of the following.
| クラス名 | Unnecessary Field (C#) | ||||||
|---|---|---|---|---|---|---|---|
| 日本語クラス名 | Unnecessary Field (C#) | ||||||
| クラス分類 | 信頼性 (reliability) | ||||||
| ニーモニック | CSHARP.STRUCT.UNFLD | ||||||
| カテゴリー |
|
||||||
| 対応言語 | C# で利用可能です。 |
||||||
| 有効/無効設定 | このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル
(configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Unnecessary Field (C#)" |
namespace DocumentationExamples
{
public class ImproperField
{
public static void Main(string[] args)
{ }
private int[] array = new int[100];
private int counter; // "Unnecessary Field (C#)" warning issued here
// - 'counter' should be replaced by local variables.
public void Clear()
{
for (counter = 0; counter < array.Length; counter++)
array[counter] = 1;
counter = 0; // "Unnecessary Field (C#)" warning issued here
// - value never used
}
private int field1; // "Unnecessary Field (C#)" warning issued here
// - field1 only used in constructors
public ImproperField(){
field1 = 0;
field1 = field1+1;
}
}
}
These issues can be resolved by removing the counter field (using a local variable instead), and by marking field1 as readonly.
namespace DocumentationExamples
{
public class ImproperField
{
public static void Main(string[] args)
{ }
private int[] array = new int[100];
public void Clear()
{
for (int counter = 0; counter < array.Length; counter++)
array[counter] = 1;
}
private readonly int field1;
public ImproperField(){
field1 = 0;
field1 = field1+1;
}
}
}
Use variables local to the methods and the constructors instead of fields.
設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。