More fluent C# / .NET

后端 未结 21 1352
名媛妹妹
名媛妹妹 2020-12-23 22:51

A co-worker of mine came up with this and I wonder what others think? Personally, I find it interesting but wonder if it is too big a departure? Code examples below. Extensi

21条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 23:54

    Regarding a "Fluent Interface" C# already has a great syntax for initializers which is (IMHO) better that trying to use the fluent style. Of course, in your example you are not initializing an new object, you are changing an existing one. My whole expertise with Fluent interfaces comes from a 30 second scan of wikipedia, but I think that JeeBee's answer is more in the spirit of Fluent programming, though I might change things slightly:

    Page.FindDropDownList("LocationDropDownList")    
      .setVisible(true)    
      .setAdminSelectedValue("111")
      .setSelectedValue("123")
    

    One could argue that this is more readable, especially for a language without Properties, but I still think it doesn't make sense in C#.

提交回复
热议问题