无法在 VS 2022 中调用 main 函数

问题描述 投票:0回答:1

在 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(); 
        }
    }
}
c# function delegates visual-studio-2022 program-entry-point
1个回答
0
投票

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.