Quantcast
Channel: Delphi – The Wiert Corner – irregular stream of stuff
Viewing all articles
Browse latest Browse all 1440

Marking code as obsolete/deprecated in C# and Delphi

$
0
0

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


Filed under: .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, C# 6 (Roslyn), Delphi, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 6, Delphi 7, Delphi 8, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Development, Software Development

Viewing all articles
Browse latest Browse all 1440

Trending Articles