[Proposal][NotNull Delegates and Events] #2159
-
C# 8 introduces NotNull Reference Types My proposal instead of doing the following: using System;
namespace SimpleConsoleApp
{
class Client
{
public Client(Server server)
{
server.ServerActionEvent += OnServerActionEvent;
}
public void OnServerActionEvent(int eventId)
{
Console.WriteLine($"OnServerActionEvent(int eventId[{eventId}])");
}
}
class Server
{
public event Action<int> ServerActionEvent;
public void DoSomeAction()
{
ServerActionEvent?.Invoke(1);
}
}
class Program
{
static void Main(string[] args)
{
Server server = new Server();
Client client = new Client(server);
server.DoSomeAction();
}
}
} Lets introduce implicit Not Null Events: using System;
namespace SimpleConsoleApp
{
class Client
{
public Client(Server server)
{
server.ServerActionEvent += OnServerActionEvent;
}
public void OnServerActionEvent(int eventId)
{
Console.WriteLine($"OnServerActionEvent(int eventId[{eventId}])");
}
}
class Server
{
public event Action<int> ServerActionEvent;
public void DoSomeAction()
{
ServerActionEvent(1);
// Under the hood it will call the following code as previously
// ServerActionEvent?.Invoke(1);
}
}
class Program
{
static void Main(string[] args)
{
Server server = new Server();
Client client = new Client(server);
server.DoSomeAction();
}
}
} The same for delegates (Not Null Delegates) What do you think guys ? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
It looks like you're proposing silently changing the behavior of existing legal syntax. |
Beta Was this translation helpful? Give feedback.
-
Yes, but we try to fix the billion dollar mistake, don't we ? If we will change syntax a little bit who lose ? |
Beta Was this translation helpful? Give feedback.
-
Everyone who sees the new code, uses it with an old compiler and gets a ticking bomb in their code. With any other feature, attempting to use it with an old compiler will result in a compile-time error, which is a much better experience.
Yes, but we're doing it very carefully, and we're not actually changing the behavior of existing programs (only which warnings they produce and even then, only if you opt-in). |
Beta Was this translation helpful? Give feedback.
Everyone who sees the new code, uses it with an old compiler and gets a ticking bomb in their code. With any other feature, attempting to use it with an old compiler will result in a compile-time error, which is a much better experience.
Yes, but we're doing it very carefully, and we're not actually changing the behavior of existing programs (only which warnings they produce and even then, only if you opt-in).