【发布时间】:2017-04-04 15:31:09
【问题描述】:
伙计们,我需要一些帮助, 我有错误 第 50 行:CS0236 C# 字段初始化程序无法引用非静态字段、方法或属性 第 51 行:CS0236 C# 字段初始化程序无法引用非静态字段、方法或属性
namespace ConsoleApplication5
{
public delegate string Metodo(string t1, string t2);
class Program
{
static void Main(string[] args)
{
}
}
public class ExemploDel
{
public static string NomeMetodo(string t1, string t2)
{
return t1 + t2;
}
Metodo meuMetodo = NomeMetodo;
}
public class Metodos
{
public string Method1(string tx1, string tx2)
{
return tx1 + tx2;
}
public string Method2(string tx1, string tx2)
{
return tx2 + tx1;
}
}
public class DelegatesEx
{
public static string NomeMetodo(string t1, string t2)
{
return t1 + t2;
}
Metodos obj = new Metodos();
Metodo m1 = obj.Method1;
Metodo m2 = obj.Method2;
Metodo m3 = NomeMetodo;
}
}
【问题讨论】:
-
您是否阅读了错误信息?这很清楚。如果你还是不明白,你试过谷歌搜索吗?这个错误非常常见。给您一些一般性的指示:您在 DelegatesEx 中
NomeMetodo之后的代码不在方法内。您还可以像引用属性一样引用方法。
标签: c#