【发布时间】:2023-03-19 22:20:02
【问题描述】:
我已经阅读了相当多的内容,并且有很多非常相似的问题的答案,但即使遵循这些问题,我也无法让它发挥作用。扩展方法是静态的,公共的,有this,在同一个命名空间中,所以不需要导入......我错过了什么?我的 C# 不太好。
namespace LDB
{
public enum Neg { isNegated, notNegated };
static class NegStringifier {
public static string ToString(this Neg n) {
string res = n switch {
Neg.isNegated => "flowers",
Neg.notNegated => "kittens",
//_ => null
};
return res;
}
}
public class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(Neg.isNegated.ToString());
System.Console.WriteLine(Neg.notNegated.ToString());
...
输出:
isNegated
notNegated
先道歉,我知道这将是一件微不足道的事情,但我看不出是什么。
【问题讨论】:
-
我想是的!请您关闭这个问题,如果您想删除它,我会同意的。