在vs2022中无法调用main函数

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

在 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
        }
    }
}
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.