如果方法具有过时的属性,则编译器在编译后会在代码中发出警告。
当在类中使用新方法时,如果您仍想在该类中保留旧方法,则可以通过显示一条消息来标记它已过时,该消息应使用新方法代替旧方法。
以下是显示如何使用过时属性的示例-
using System;
public class Demo {
[Obsolete("Old Method shouldn't be used! Use New Method instead", true)]
static void OldMethod() {
Console.WriteLine("这是旧方法!");
}
static void NewMethod() {
Console.WriteLine("这是新方法!");
}
public static void Main() {
OldMethod();
}
}当我们在上面设置警告消息时,它将显示以下警告-
Compilation failed: 1 error(s), 0 warnings error CS0619: `Demo.OldMethod()' is obsolete: `Old Method shouldn't be used! Use New Method instead'