Is extending String class with IsNullOrEmpty confusing?

后端 未结 12 2008
有刺的猬
有刺的猬 2020-12-06 01:40

Everyone knows and love String.IsNullOrEmpty(yourString) method.

I was wondering if it\'s going to confuse developers or make code better if we extend String class t

12条回答
  •  日久生厌
    2020-12-06 02:33

    It doesn't just look like you're calling a method on a null variable. You /are/ calling a method on a null variable, albeit one implemented through a static extension method. I had no idea extension methods (which I was already leery of) supported that. This even allows you to do crazy things like:

    public static int PowerLength(this string obj)
    {
    return obj == null ? 0 : obj.Length;
    }
    

    From where I'm standing now, I would classify any use of an extension method on a null reference under considered harmful.

提交回复
热议问题