Event, Delegate, Action, Func, predicate

Markus Freitag 3,361 Reputation points


public delegate void OnMessageEventHandler(object sender, string received);
public event OnMessageEventHandler EvMessageReceived;

public delegate void OnLoggingEventHandler(object sender, string message, LogEventType eventType);
public event OnLoggingEventHandler EvClientEvent;

public delegate void OnClientStateChangedEventHandler(object sender, bool connected);
public event OnClientStateChangedEventHandler EvStateChanged;

I have existing C# code and need to implement extensions. How can I write the code better? Or shorter?
Do you have an overview
Old and new spelling.

Thanks in advance.

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,958 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 32,766 Reputation points


    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.

    Or shorter?
    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.

    Action, Func
    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.

    1 person found this answer helpful.