C# 中哪些属性已过时?

来源:这里教程网 时间:2026-02-21 16:28:55 作者:

c# 中哪些属性已过时?

如果方法具有过时的属性,则编译器在编译后会在代码中发出警告。

当类中使用新方法并且您仍想保留该方法时类中的旧方法,您可以通过显示应使用新方法而不是旧方法的消息将其标记为过时。

以下是显示如何使用过时属性的示例 - p>

using System;
public class Demo {
   [Obsolete("Old Method shouldn't be used! Use New Method instead", true)]
   static void OldMethod() {
      Console.WriteLine("This is the old method!");
   }
   static void NewMethod() {
      Console.WriteLine("This is the new method!");
   }
   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'

相关推荐