Search code examples
c#.netintellisense

How to show non implemented methods in API without throwing an exception and prevent compilation


I am writing a core API that will be used by client developer.

There are some methods that are still not developed but I need them to be in the API so the developer can see them in the Intellisense.

In addition to the API documentation I am providing to the developer, I need him to know during the development that a method is not yet implemented but it exist.

Is there a way to inform the developer that the method is not yet implemented without throwing NotImplementedException and if he will try to use the method it will not compile?

For example:

public class API
{
    public void PrintToPDF()
    {
          // do not throw NotImplementedException
    }
}


public class Client
{
     public void Print()
     {
          API api = new API();
          api.PrintToPDF();        // shouldn't compiled but can be see in intellisense. It can show a tooltip that it is in being developed. 
     }
}

Solution

  • Use the obsolete attribute, it can generate both a warning or an error as you define it.

    using System;
    using System.Reflection;
    
    public class Example
    {
       // Mark OldProperty As Obsolete.
       [ObsoleteAttribute("This property is for future use", false)] 
       public static string OldProperty
       { get { return "The old property value."; } }
    
       public static string NewProperty
       { get { return "The new property value."; } }
    
       // Mark CallOldMethod As Obsolete.
       [ObsoleteAttribute("This method is for future use", true)] 
       public static string CallOldMethod()
       {
          return "You have called CallOldMethod.";
       }
    
       public static string CallNewMethod() 
       {   
          return "You have called CallNewMethod.";
       }   
    
       public static void Main()
       {                 
          Console.WriteLine(OldProperty);
          Console.WriteLine();
          Console.WriteLine(CallOldMethod());
       } 
    }
    // The attempt to compile this example produces output like the following output:
    //    Example.cs(31,25): error CS0619: 'Example.CallOldMethod()' is obsolete: 
    //            'This method is for future use'
    //    Example.cs(29,25): warning CS0618: 'Example.OldProperty' is obsolete: 
    //            'This property is for future use'
    

    You can also create your own attribute.

    [Obsolete("Reserved for future use", true)]
    public class ReservedForFutureUse : System.Attribute
    {
    
    }