The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,741 other followers

Marking code as obsolete/deprecated in C# and Delphi

Posted by jpluimers on 2015/04/07

During any software life cycle, you will want to phase out some code, and most likely want to mark code to be phased out in the future.

So here are two examples on how to do that in C# and in Delphi that just shows the effects of obsoleting/deprecating code.

C#

On the C#/.NET side of things you can mark code to be phased out with the Obsolete attribute that got introduced in C# 1.1 in Visual Studio 2003.
It even allows you to add a string directing you to more appropriate usage patterns.
You can even indicate if this should just be a warning, or that the compiler should issue an error.

Though possible, it is highly unrecommended (is that even proper English?) to use the pragma warning 612/618 disable to circumvent obsolete warnings. Don’t do that!

A small example in C# that uses the .NET framework class library:

Now that IP addresses are not restricted IPv4 any more (which used to be 4 bytes that would fit in a long), but also can be IPv6 (which are 8 groups of 2 bytes totalling 16 bytes – coincidence or not the same size of a GUID), it is not a good idea to use the Address property of an IPAddress instance. So that Address property got obsoleted in .NET 1.1 (it wasn’t in .NET 1.0).
Since the Address property was usually obtained to compare two addresses, now you can use the Equals method or equality (==) operator for that.

ObsoleteConsoleApplication:

Delphi

In Delphi, you use the deprecated directive for this. You can use it to mark code as “obsolete”.

Together with platform , library and experimantal this is one of the hinting directives; the current documentation of Delphi XE6 even allows for adding a string describing the deprecation in more detail, but back in Delphi 6 when deprecated got introduced (and Borland tried the Linux adventure with their Kylix product) you just had the deprecated on its own.

A small Delphi example is about SysUtils.FileAge. It has two overloads, of which one is deprecated.

So the first call will give you a compiler warning:

program DeprecatedFileAge

–jeroen

6 Responses to “Marking code as obsolete/deprecated in C# and Delphi”

  1. smile said


    {$region ‘Conditional Compilation’}

    {$IFDEF CONDITIONALEXPRESSIONS}
    {$IF CompilerVersion > 18.5} // Delphi 2007 18.5 VER185
    {$DEFINE D2009UP} // Define for Delphi and C++Builder 2009
    {$DEFINE DCT_MSG} // Deprecated messages
    {$IFEND}
    {$ENDIF}

    {$endregion}

    function Connect: Boolean; deprecated {$IFDEF DCT_MSG}’IOracleDOA.ExecuteLogon'{$ENDIF};

  2. Marjan said

    In Delphi you can also add a message to the deprecated keyword. Syntax:

    [[deprecated(“message”)]]

    Example given in the docs :

    A(int x, int y) [[deprecated(“use the A(int,double) constructor”)]]{}

    will give a :

    W8111 Accessing deprecated entity ‘A::A(int,int)’ use the A(int,double) constructor

Leave a Reply to jpluimers Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: