C
C#2y ago
thieved

what is this operation?

discord.MessageCreated += async (s, e) =>
discord.MessageCreated += async (s, e) =>
context:
14 Replies
thieved
thievedOP2y ago
im learning C# i know what += but why is it used like this in C#?
mtreit
mtreit2y ago
That's an event You are adding an event handler to the event
mtreit
mtreit2y ago
Events - C# Programming Guide
Learn about events. Events enable a class or object to notify other classes or objects when something of interest occurs.
thieved
thievedOP2y ago
i know what an event is but i meant why is += being used for that? i come from javascript and lua so += is for adding to strings, numbers, etc. elaborate is this one of those things like 'it is what it is' ?
mtreit
mtreit2y ago
Yes, that syntax goes back to C# 1.0 over 20 years ago They wanted events to be a first class thing and to support registering multiple event handlers for a single event
thieved
thievedOP2y ago
oh okay well thats refreshing i thought id have to discover some indepth explanation because i like to know the why to things but one question what does an event look like for that to be the operation to subscribe to it? right like just calling .on() multiple times right ohhh so thats a whole type i made an event emitter in lua so thats odd to have a whole type for events what is a delegate? im new to C# lol
public event AsyncEventHandler<DiscordClient, ReadyEventArgs> Ready
{
add => this._ready.Register(value);
remove => this._ready.Unregister(value);
}
public event AsyncEventHandler<DiscordClient, ReadyEventArgs> Ready
{
add => this._ready.Register(value);
remove => this._ready.Unregister(value);
}
so add is called when an event is added? i sorta understand wait i do
rotor_
rotor_2y ago
So C# delegates are composable? Do they feed the output of one into the input of the next or do they all just get run with the outputs discarded?
thieved
thievedOP2y ago
new DiscordConfiguration() {
Token = Environment.GetEnvironmentVariable("DISCORDTOKEN"),
TokenType = TokenType.Bot
}
new DiscordConfiguration() {
Token = Environment.GetEnvironmentVariable("DISCORDTOKEN"),
TokenType = TokenType.Bot
}
sorry to ask another question but how does this work ? why isnt the object passed inside the class parameters ?
rotor_
rotor_2y ago
It's a limitation of the library - honestly that should be passed in the constructor But like moles says there's a workaround
thieved
thievedOP2y ago
couldnt they pass an object? lol
rotor_
rotor_2y ago
This is true, but I'd argue that the DiscordConfiguration won't do much without a token Oooo I didn't know that thank you!
thieved
thievedOP2y ago
omg C# and the extension for it on VSCODE has such good reference tracking
thieved
thievedOP2y ago
thieved
thievedOP2y ago
anyways, thank you sm @implicit electron ❤️
Want results from more Discord servers?
Add your server