C
C#2y ago
Dean

❔ Copying video on Delegates, why doesn't my version work?

Why does my code not work, but the code in the video i'm following does. OhNo
26 Replies
Angius
Angius2y ago
Idk, what's the error?
Dean
Dean2y ago
Angius
Angius2y ago
Well, the difference, then, is that Start() in the video is not static, while your Main() is You can make your Testdelegatefunction field static Same for the TestBullshitDelegate method
Dean
Dean2y ago
hmm so why did main being static stop it working? i thought that just means you cant instantiate a new object of it is there another way around it without making the field and method static?
Dean
Dean2y ago
Dean
Dean2y ago
ok that also fixes it thanks for the help
Angius
Angius2y ago
There is a way, creating your own, separate non-static main Gimme a second
public class App
{
public void Run(string[] args)
{
// your actual code goes here
}
}
public class App
{
public void Run(string[] args)
{
// your actual code goes here
}
}
public class Program
{
public static void Main(string[] args)
=> new App().Run(args);
}
public class Program
{
public static void Main(string[] args)
=> new App().Run(args);
}
And you only work with the App class, not touching Program and Main()
Dean
Dean2y ago
=> new App().Run(args); what's this syntax called lambda expression? these seem to have different rules in different scenarios dont have them all memories properly
Angius
Angius2y ago
Expression-bodied method
Dean
Dean2y ago
it doesnt require something on the left of it?
Angius
Angius2y ago
The signature of the method is to the left
Dean
Dean2y ago
thats usually the arguments on the left right?
Angius
Angius2y ago
public static void Main(string[] args)
=> new App().Run(args);
public static void Main(string[] args)
=> new App().Run(args);
is the same as
public static void Main(string[] args)
{
new App().Run(args);
}
public static void Main(string[] args)
{
new App().Run(args);
}
It's not an anonymous function/lambda Just a shorthand for writing one-liner methods
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
int Sum(int a, int b) => a + b;

Sum(6, 4)
int Sum(int a, int b) => a + b;

Sum(6, 4)
Result: int
10
10
Compile: 432.017ms | Execution: 36.994ms | React with ❌ to remove this embed.
Dean
Dean2y ago
i wish they would pick something different than => when its a different thing
Angius
Angius2y ago
Yeah, that's a valid concern
Dean
Dean2y ago
so its saying execute/return the following but it only works if its 1 line?
Angius
Angius2y ago
Yep
Dean
Dean2y ago
alright cool gotta smash that into my brain
Angius
Angius2y ago
Or, well, one statement
int Whatever() => things
.Select(x => x.Count)
.Where(x => x > 18)
.Aggregate((a, b) => a + b, 0);
int Whatever() => things
.Select(x => x.Count)
.Where(x => x > 18)
.Aggregate((a, b) => a + b, 0);
for example is multiple lines, but it's a single statement
Dean
Dean2y ago
right, it's everything chainged up in one statement ended by one ; how are you getting the comments to format your code? int Whaetver() => 1 is there a page somewhere that shows you i cant see it
Angius
Angius2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Dean
Dean2y ago
$codegif
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts