How can I write the code better?
Start off with a plan, many will have an idea and start coding which is wrong as an idea is followed by a plan. Once there is a plan there are multiple paths.
The first path is to create classes which represent the
plan then write unit test methods to test code in the classes. Now these classes may be in a class project which a frontend project consumes or the classes are in the frontend.
The second path, this is the one most avoided as it typically takes double the time as path 1 (when path 1 goes as planned and path 2 goes as planned) is TDD (Test Driven Development). When things don't go as planned with path 1, then path 1 can take triple or more time to completed.
Shorter is not always better. For example, the following code is short and generic which some will see as great (without unit test) then when it comes time to debug code this code can be a pain in the butt. Yet if coded properly and properly test
short is good.
public static bool Between<T>(this T value, T lowerValue, T upperValue) where T : struct, IComparable<T> => Comparer<T>.Default.Compare(value, lowerValue) >= 0 && Comparer<T>.Default.Compare(value, upperValue) <= 0;
delegates and events
If perhaps you have a class with static methods then static declarations of events is good. Here is an example picked a random which shows using static methods and events along with a slimmed down version to raise the event.
Here is an efficient use in a projection used in this method. The projection used as a predicate allows
shorter code without sacrificing readability and debugging.
Going back to shorter code, there are some ways to implement something like a switch that can be very difficult to read, see what you think of the switches in this class.
How one implements an action or func or not can make code less efficient but then again the code may run just fine,
Now here is an Action coupled with a switch, to some this makes sense while others it makes sense for a short time then two weeks later, what the heck is that, what have I done?
Bottom line, spend time planning out your project, use or don't use unit test and stop thinking shorter code is better than longer code, that can boil down to experience and refactoring.
Note: If code is not properly formatted above the forum is not rendering code properly.
As recommended in your other threads, take the time to read the reference documentation so you understand the definitions. Func is a delegate that returns a value. Action is a delegate that returns void. A Predicate is similar to a Func that returns true or false.
A delegate is like a method variable. The delegate can be assigned to any method where the parameters and return types match.
The C# Programming Guide covers delegate well and explains when to use delegates with examples.
Then the question is answered. Thanks!