In VS 2022, the entry point of the code public static void main no longer exist.
I was trying to implement a simple delegate but no output is shown on the console:
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();
}
}
}
In
Main(), you need to create an INSTANCE of your classMyClassDel, and then make your delegate point to the theDoSomething()method of that particular instance:Alternatives...
(1) Make
DoSomething()a static method in classMyClassDel:(2) Make
DoSomething()a static method in classProgram: