How to mark/warn a method is obsolete

Asked

Viewed 450 times

3

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

inserir a descrição da imagem aqui

Like leaving the method obsolete below:

    /// <summary>
    /// 
    /// </summary>
    /// <param name="a"></param>
    /// <exception cref=""></exception>
    /// <returns></returns>
    public string MeuMetodo(string a)
    {
        return a;
    }

1 answer

3


Only use the attribute Obsolete.

It is also possible to pass a custom message to the Warning and as the second parameter a boolean which defines whether the use of this method will only generate a Warning or a mistake.

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) { }
}

In use

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

Classe.OutroMetodo("B");
// Gera um erro e interrompe a compilação
  • Wow! Just? Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.