在 vs2022 中,代码 public static void main 的入口点不再存在。
我试图实现一个简单的委托,但控制台中没有显示任何输出
using System;
namespace ConsoleApp1
{
public delegate void SomeMethodPointer(); // delegate definition
public class MyClassDel
{
public void DoSomething() // function which i am trying to call through delegate
{
Console.WriteLine("In the function");
}
}
public class Program
{
public static void Main(string[] args)//created this here as program,cs is empty in my console app
{
SomeMethodPointer obj = new SomeMethodPointer(DoSomething); // Initialize the delegate with a method reference
obj.Invoke(); // Call the delegate
}
}
}
在
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();
}