Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the C# equivalent of perl warn?

Tags:

c#

In Perl I can say

use warnings;
warn "Non-fatal error condition, printed to stderr.";

What is the equivalent of this in C# ASP.NET? Specifically what I'm looking for is something so that other members of my team can know when they're still using a deprecated routine. It should show up at run time when the code path is actually hit, not at compile time (otherwise they'd get warnings about code sitting at a compatibility layer which ought to never run anyway.)

My best option so far is to use Trace, which feels like a bad hack.

like image 311
sorpigal Avatar asked Mar 01 '10 19:03

sorpigal


People also ask

What is meant by C?

1. the third letter of the English alphabet, a consonant. 2. any spoken sound represented by the letter C or c, as in cat, race, or circle.

Is C or C+ Better?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.

What type language is C?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C and why it is used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

EDIT: I hadn't seen the bit in the question saying you wanted it at execution time.

The problem about writing out data execution time is that you need to know where to write it. What logging does your application use in general? Use the same form of logging as that, basically.

I would still favour the compile-time option listed below, however - you can turn off specific warnings in the compatibility layer using #pragma warn disable/restore, but it'll make it a lot easier to spot the problems than hoping someone reads a log file...


Old answer

Use the [Obsolete] attribute on any type or member. You can decide whether this should end up being a warning or an error. For example:

using System;

class Test
{
    static void Main()
    {
        OldMethod();
        BadMethod();
    }    

    [Obsolete("Use something else instead")]
    static void OldMethod() {}

    [Obsolete("This would destroy your machine!", true)]
    static void BadMethod() {}
}

Compiling this gives:

Test.cs(7,9): warning CS0618: 'Test.OldMethod()' is obsolete: 'Use something else instead' Test.cs(8,9): error CS0619: 'Test.BadMethod()' is obsolete: 'This would destroy your machine!'

Ideally, the message should explain what the effects of continuing to use the method would be, and the suggested alternative.

like image 42
Jon Skeet Avatar answered Sep 30 '22 13:09

Jon Skeet


Use ObsoleteAttribute. Decorate it over any method you want to mark as deprecated. They'll get a warning in their errors window, but the app will still compile.

public class Thing
{
     [Obsolete]
     public void OldMethod() { Console.WriteLine("I'm Old"); }
}
like image 84
David Morton Avatar answered Sep 30 '22 13:09

David Morton