I have a class with a member that I need to keep for stale code, and I need to mark it as stale so that the new code does not use it (without warning).
Let's say the class looks like this:
class MyClass { [Obsolete] private string _old = "..."; [Obsolete] public string Old { get { return _old; } } }
I declare that the _old member _old is out of date to ensure that the new code inside the class does not use this field.
I also declare that the Old property is deprecated to ensure that code outside the class does not use the property.
When I compile this, I get a warning in the getter property that _old deprecated. I thought the compiler silently ignores this, since the property itself is deprecated.
Am I missing something or do I need to add #pragma warning disable/restore for deprecated member fields wherever they are used (although the method / property itself is marked as deprecated)?
The reason “I thought the compiler silently ignores this” is because it seems to be doing this for legacy classes:
[Obsolete] public class MyObsoleteClass { public string DoSomething() {
As @Heinzi replied: it looks like this is due to a bug in Visual Studio. I sent a connection report:
https://connect.microsoft.com/VisualStudio/feedback/details/1146809
It turns out that the error in Visual Studio is not limited to accessing an obsolete field from a property.
Access to an obsolete property from an obsolete method should not trigger a warning:
public class Class2 { [Obsolete] public string Property { get; set; } [Obsolete] public void Method() { this.Property = "value";
You should not do this from another class:
public class Class3 { [Obsolete] public string Property { get; set; } } public class Class4 { [Obsolete] public string Method() { return new Class3().Property;
Interestingly, it works in the next class, and when you add this class, other warnings (from Class4 and Class2 ) will magically disappear.
public class Class5 { [Obsolete] public void Method() {