How to mark / warn that a method is obsolete

3

I've noticed several times in Visual Studio about obsolete methods of some components / libraries:

Asforexampleleavingthemethodbelowobsolete:

///<summary>//////</summary>///<paramname="a"></param>
    /// <exception cref=""></exception>
    /// <returns></returns>
    public string MeuMetodo(string a)
    {
        return a;
    }
    
asked by anonymous 22.02.2017 / 20:42

1 answer

3

Only use the Obsolete .

It is also possible to pass a custom message to the warning and as a second parameter a boolean that defines whether the use of this method will only generate a warning or an error .

Example

public static class Classe
{
    [Obsolete("Use o novo método")]
    public static void MeuMetodo(string a) { }

    [Obsolete("Use o novo método", true)]
    public static void OutroMetodo(string a) { }
}

I do not use

Classe.MeuMetodo("A");
// Gera um warning "Use o novo método"

Classe.OutroMetodo("B");
// Gera um erro e interrompe a compilação
    
22.02.2017 / 20:44