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.ID.SHADOW : Shadowed Identifier (C#)

Summary

A class defines a field with the same name as another in a superclass.

C# classes can be extended and methods overridden. Sometimes, it is possible that methods are redefined in a way that seems an overriding but is actually the definition of another, distinct method. This is often cause of ambiguities and bugs. Moreover, fields in C# cannot be overridden and a subclass may add a synonym field of a superclass: the class ends up having two fields with the same name. This is often a source of ambiguities and might lead programmers to think that the field is actually overridden at all uses, while distinct synonym fields are used at different program points.

This checker finds ambiguities and bugs in the extension of classes, method overriding and fields redefinition.

Properties

Class Name Shadowed Identifier (C#)
Significance reliability
Mnemonic CSHARP.ID.SHADOW
Categories
CWE CWE:1076 Insufficient Adherence to Expected Conventions
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="Shadowed Identifier (C#)"

Example

The following code sample has two classes named BadExtensionC:

using System;


namespace DocumentationExamples
{

    public class BadExtension
    {
        public static void Main(string[] args)
        { }

        public readonly int k = 13;
        public BadExtension() { }
        public virtual float MyMethod(int a, float f)
        {
            return a + f + k;
        }
        public virtual void AnotherMethod(BadExtensionC par) { }

    }
    public class BadExtensionC { }

    public class BadExtensionSub2 : BadExtension
    {
        public BadExtensionSub2() { }
        public float Mymethod(int a, float f)                                  // Method Names Differ Only in Case (C#) warning issued here 
                                                                               // - may have been intended to override BadExtension.MyMethod
        {
            return a + f;
        }
    }
    public class BadExtensionSub3 : BadExtension
    {
        public readonly new int k = 17;                                        // Shadowed Identifier (C#) warning issued here 
                                                                               // - there is a non-private field k in class BadExtension
        public BadExtensionSub3() { } 
        public void AnotherMethod(BadExtensionNamespace.BadExtensionC par) { } // Non-overriding Method Signature (C#) warning issued here 
                                                                               // - may have been intended to override BadExtension.AnotherMethod(), but the parameter types don't match.
    }

}
namespace BadExtensionNamespace
{
    public abstract class BadExtensionC { }
}

Resolution

Check if a method redefinition should rather be an overriding. Check if a synonym field, already defined in a superclass, should be removed or renamed.

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/.