在 VS 2022 中,代码 public static void main 的入口点不再存在。
我试图实现一个简单的委托,但控制台上没有显示任何输出:
using System;
namespace ConsoleApp1
{
public delegate void SomeMethodPointer(); // delegate definition
public class MyClassDel
{
// function which I am trying to call through a delegate
public void DoSomething()
{
Console.WriteLine("In the function");
}
}
public class Program
{
// created this here as program,cs is empty in my console app
public static void Main(string[] args)
{
// Initialize the delegate with a method reference
SomeMethodPointer obj = new SomeMethodPointer(DoSomething);
// Call the delegate
obj.Invoke();
}
}
}
在
Main()
中,您需要创建类 MyClassDel
的实例,然后让您的委托指向该特定实例的 DoSomething()
方法:
static void Main(string[] args)
{
MyClassDel mcd = new MyClassDel();
SomeMethodPointer obj = new SomeMethodPointer(mcd.DoSomething);
obj.Invoke(); // Call the delegate
Console.ReadLine();
}