是"这&Q​​UOT空;一个可接受的使用扩展方法?可接受、方法、QUOT、UOT

2023-09-04 07:43:56 作者:终遇

所以,我真的很喜欢使用的扩展方法..也许有点太多了。所以,我要问我最新的享受,以确保我不会太远。

情景是,我们有一个被传递了的Guid?变量,如果变量为空或 Guid.Empty ,那么我们就需要使用不同的GUID。所以,我写了一个扩展方法,使其读起来像英语:

 内部静态的Guid OrIfEmpty(此GUID?GUID,其他的Guid)
    {
        如果(!guid.HasValue || guid.Value == Guid.Empty)
        {
            返回等;
        }
        返回guid.Value;
    }
 

这自动意味着空这个不会抛出异常。举例来说,这将工作:

 ((GUID)空?).OrIfEmpty(其它);
 

这是不可能的,而无需使用扩展方法,在我看来可以说是相当具有误导性。然而,这只是这么简洁,干净!那么,你有什么感想?这是一个可以接受的事情或者会不会太混乱,其他程序员?

另外,我敢肯定还会有其他的场景,我做这样的事情,并检查为空,但是这是一个最好的例子我现在。

注:我真的不问这个的Guid 在特定的业务?我问更多有关实施的总体格局(有一个扩展方法,其中可以是

解决方案   

这是不可能的,而无需使用扩展方法

当然是,只要是正规的静态方法调用(这是所有扩展方法):

  

在我看来,可以说是相当误导

我同意,因为它的看起来的你调用一个实例的实例方法。这可能会更糟:

 字符串s = NULL;
字符串n = s.OrIfEmpty(空);
 

乍一看,这看起来像一个明显的的NullReferenceException 等待发生,但它编译和作品的设计。

由于你的问题其实只是征求意见,有没有一个正确的答案,但我肯定会保持谨慎,文件扩展方法来指示参数可以是。或(如@quezalcoatl暗示),其重命名为更加明确,它支持值:

 内部静态的Guid OrIfNullOrEmpty(此GUID?GUID,其他的Guid)
 

So, I really enjoy using extension methods.. maybe a bit too much. So, I'm going to ask about my latest enjoyment to ensure that I'm not going too far.

Scenario is that we have a Guid? variable that gets passed in. If the variable is null or Guid.Empty, then we want to use a different Guid. So, I wrote an extension method to make it read like English:

    internal static Guid OrIfEmpty(this Guid? guid, Guid other)
    {
        if (!guid.HasValue || guid.Value == Guid.Empty)
        {
            return other;
        }
        return guid.Value;
    }

This automatically implies that a "null this" will not throw an exception. For instance, this will work:

((Guid?)null).OrIfEmpty(other);

This is not possible without using extension methods, and in my opinion can be quite misleading. However, it's just so concise and clean! So, what do you think? Is this an acceptable thing to do or could it be too confusing to other programmers?

Also, I'm sure there will be other scenarios where I do things like this and checking this for null, but this is the best example I have right now.

Note: I'm not really asking about this Guid? business in particular. I'm asking more about the overall pattern implemented (having an extension method where the this can be null)

解决方案

This is not possible without using extension methods

Sure it is, just make is a regular static method call (which is all extension methods are):

in my opinion can be quite misleading

I agree, since it looks like you're calling an instance method on a null instance. It could be worse:

string s = null;
string n = s.OrIfEmpty("empty");

At first glance this looks like an obvious NullReferenceException waiting to happen, but it compiles and works as designed.

Since your question is really just soliciting opinions, there's not one right answer, but I certainly would be cautious and document the extension method to indicate that the this parameter could be null. Or (as @quezalcoatl implies) rename it to be more explicit that it supports null values:

internal static Guid OrIfNullOrEmpty(this Guid? guid, Guid other)