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
C#


CSHARP.TYPE.ARRAYTOSTRING : toString on Array (C#)

Summary

ToString() is called over an array.

Arrays are instances of System.Object in C#. As a consequence, all methods of System.Object can be invoked on arrays, However, this is not always sensible. In particular, calls to ToString() yield a string derived from the unique identifier of the object, which is likely to be useless for the programmer and not her intent when she called ToString().

It is possible that this warning is the consequence of refactoring, that replaced a collection class with an array but left the method calls unchanged.

Properties

Class Name toString on Array (C#)
Significance reliability
Mnemonic CSHARP.TYPE.ARRAYTOSTRING
Categories
CWE CWE:440 Expected Behavior Violation
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="toString on Array (C#)"

Example

using System;

namespace DocumentationExamples
{

    public class CallsOnArray
    {
        private static CallsOnArrayA[] array;
        public static void Main(string[] args)
        {
            array = new CallsOnArrayA[args.Length];
            for (int pos = 0; pos < args.Length; pos++)
                array[pos] = new CallsOnArrayA(args[pos]);
            Foo(array);
            Console.WriteLine(array);  // "toString on Array (C#)" warning issued here 
                                       // - call is inside Console.WriteLine() 
        }
        private static void Foo(object o)
        {
            string s = o.ToString();   // "toString on Array (C#)" warning issued here 
            Console.WriteLine(s);
        }
    }
  
    public class CallsOnArrayA
    {
        private int f;
        public CallsOnArrayA(string s)
        {
            f = s.Length;
        }
        public override string ToString()
        {
            return f.ToString();
        }
    }
}

One solution is to use a loop:

for (int pos = 0; pos < array.Length; pos++)
    Console.WriteLine(array[pos]);

Resolution

Replace the call with a call to that return a string with the elements of array or with an explicit iteration over the elements of the array.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.