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 |
Two arrays are compared with Equals().
This checks whether or not they are exactly the same object. If the intent is to check whether they have the same contents, perform element-wise comparison instead.
This checker identifies incorrect or inefficient comparisons through Equals() or ==. In some cases, these comparisons are wrong: for instance, strings should be compared for equality through Equals() rather than through ==. Classes can be safely compared through == instead. In other cases, these comparisons can be replaced by more efficient code.
| クラス名 | equals on Array (C#) | |||
|---|---|---|---|---|
| 日本語クラス名 | equals on Array (C#) | |||
| クラス分類 | 信頼性 (reliability) | |||
| ニーモニック | CSHARP.COMPARE.EQARRAY | |||
| カテゴリー |
|
|||
| 対応言語 | C# で利用可能です。 |
|||
| 有効/無効設定 | このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル
(configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="equals on Array (C#)" |
using System;
using System.Diagnostics;
namespace DocumentationExamples
{
public class BadEq
{
public static bool verbose;
private readonly static string[] arr = new string[] { "verbose" };
public static void Main(string[] args)
{
if (args.Length > 0)
Debug.Assert(!args[0].Equals("")); // Comparison to Empty String (C#) warning issued here
if (args.Length > 0 && args[0] == "verbose")
verbose = true;
if (args.Equals(arr)) // equals on Array (C#) warning issued here
verbose = true;
if (args.Length > 0)
{
BadEq m = new BadEq();
Console.WriteLine(m.Test(args[0]));
Console.WriteLine(m.WeAreRedefined());
}
}
private bool Test(object o)
{
return o == this; // == Always Fails (C#) warning issued here
// - operands have incompatible types
}
public bool WeAreRedefined()
{
return !GetType().Equals(typeof(BadEq)); // Missing Equals Override (C#) warning issued here
}
public bool EqualityTest()
{
object name = "myname";
char[] values = { 'm', 'y', 'n', 'a', 'm', 'e' };
object myName = new string(values);
return name == myName; // Should Use equals() Instead of == (C#) warning issued here
}
}
}
In this example, the programmer should rewrite the code as follows:
public static bool verbose;
private readonly static string[] arr = new string[] { "verbose" };
public static void Main(string[] args)
{
if (args.Length > 0)
Debug.Assert(!string.IsNullOrEmpty(args[0]));
if (args.Length > 0 && args[0].Equals("verbose"))
verbose = true;
if (args.SequenceEqual(arr))
verbose = true;
if (args.Length > 0)
{
BadEq m = new BadEq();
//Console.WriteLine(m.Test(args[0]));
Console.WriteLine(m.WeAreRedefined());
}
}
public bool WeAreRedefined()
{
return GetType() != typeof(BadEq);
}
Check if the equality is actually wrong or can be replaced by more optimized code.
設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。
To report problems with this documentation, please visit https://support.codesecure.com/.