Evil use of Maybe monad and extension methods in C#?

前端 未结 8 1872
面向向阳花
面向向阳花 2020-12-07 17:11

edit 2015 This question and its answers are no longer relevant. It was asked before the advent of C# 6, which has the null propagating opertor (?.)

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 17:20

    I'm not too crazy about either solution. What was wrong with ashorter version of the original:

    string activeControlName = null;
    if (Form.ActiveForm != null)
        if (Form.ActiveForm.ActivControl != null) activeControlname = activeControl.Name;
    

    If not this, then I would look at writing a NotNullChain or FluentNotNull object than can chain a few not null tests in a row. I agree that the IfNotNull extension method acting on a null seems a little weird - even though extension methods are just syntactic sugar.

    I think Mark Synowiec's answer might be able to made generic.

    IMHO, I think the C# core team should look at the this "issue", although I think there are bigger things to tackle.

提交回复
热议问题