Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obsolete member field with property accessor (Visual Studio bug)

Tags:

c#

obsolete

I have a class with a member that I need to keep for legacy code and I need to mark it as obsolete so that new code doesn't use it (without getting a warning).

Let's say the class looks like this:

class MyClass
{
    [Obsolete]
    private string _old = "...";

    [Obsolete]
    public string Old
    {
        get { return _old; }
    }
}

I'm declaring that the member field _old is obsolete to make sure that new code inside the class do not use that field.

I'm also declaring that the property Old is obsolete to make sure that code outside of the class do not use the property.

When I compile this I get a warning in the property getter saying that _old is obsolete. I thought that the compiler would silently ignore this since the property itself is obsolete.

Am I missing something or do I need to add #pragma warning disable/restore for obsolete member fields everywhere they're used (even though the method/property itself is marked as obsolete)?


The reason that "I thought that the compiler would silently ignore this" is because it seems to do so for obsolete classes:

[Obsolete]
public class MyObsoleteClass
{
    public string DoSomething()
    {
        // No warning here, since the class itself is obsolete
        return new MyClass().Old;
    }
}

As @Heinzi answered: this seems to be due to a bug in Visual Studio. I've filed a report on connect:

https://connect.microsoft.com/VisualStudio/feedback/details/1146809


It turns out that the bug in Visual Studio is not just limited to accessing an obsolete field from a property.

Accessing an obsolete property from an obsolete method should not yield a warning:

public class Class2
{
    [Obsolete]
    public string Property { get; set; }

    [Obsolete]
    public void Method()
    {
        this.Property = "value"; // <-- Incorrect warning reported
    }
}

Neither should doing so from another class:

public class Class3
{
    [Obsolete]
    public string Property { get; set; }
}

public class Class4
{
    [Obsolete]
    public string Method()
    {
        return new Class3().Property; // <-- Incorrect warning reported
    }
}

Interestingly, it works in the following class and when adding this class the other warnings (from Class4 and Class2) will magically disappear.

public class Class5
{
    [Obsolete]
    public void Method()
    {
        // No warning reported here, which is good.
        // This magically makes the other warnings disappear too!
        new Class2().Method();
    }
}
like image 884
Mårten Wikström Avatar asked Nov 01 '22 09:11

Mårten Wikström


1 Answers

Your code is fine, and your understanding of how the Obsolete attribute should work is correct: If you look at the "Output" tab after compilation, you will note that the compiler does not output a warning for your case (but will output a warning if you remove the Obsolete attribute from your property, as expected).

You are right, though, that Visual Studio sometimes displays a warning after making arbitrary changes to the code. This seems to be a bug in Visual Studio. If you can still reproduce it with the most current version, I would suggest that you file a bug report on http://connect.microsoft.com.

like image 71
Heinzi Avatar answered Nov 15 '22 07:11

Heinzi