C#

C#

By DeepSource

Built-in entity shadowed by a declaration CS-W1021

Bug risk

There already exists an entity with the same name as the one highlighted in the System namespace. This effectively shadows the built-in entity and prevents you from accessing or instantiating the said built-in entity including the places where the entity is imported, which can cause unintended effects, that can be difficult to debug. It is therefore recommended that you rename your declaration to avoid this issue.

Examples

Bad Practice

using System;

namespace Foo
{
    class Random // Hides `System.Random`
    {
    }

    class Bar
    {
        public static void Main()
        {
            var r = new Random();
        }
    }
}

Recommended

using System;

namespace Foo
{
    class MyRandomNumberGenerator
    {
    }

    class Bar
    {
        public static void Main()
        {
            var r = new MyRandomNumberGenerator();
        }
    }
}