Best practice of using the “out” keyword in C#

后端 未结 10 2024
暗喜
暗喜 2020-12-16 11:53

I\'m trying to formalise the usage of the \"out\" keyword in c# for a project I\'m on, particularly with respect to any public methods. I can\'t seem to find any best practi

相关标签:
10条回答
  • 2020-12-16 12:03

    This is what the .NET Framework Developer's Guide has to say about out parameters:

    Avoid using out or reference parameters.

    Working with members that define out or reference parameters requires that the developer understand pointers, subtle differences between value types and reference types, and initialization differences between out and reference parameters.

    But if you do use them:

    Do place all out parameters after all of the pass-by-value and ref parameters (excluding parameter arrays), even if this results in an inconsistency in parameter ordering between overloads.

    This convention makes the method signature easier to understand.

    0 讨论(0)
  • 2020-12-16 12:03

    Your approach is better than out, because you can "chain" calls that way:

    DoSomethingElse(DoThing(a,b).Result);
    

    as opposed to

    DoThing(a, out b);
    DoSomethingElse(b);
    

    The TryParse methods implemented with "out" was a mistake, IMO. Those would have been very convenient in chains.

    0 讨论(0)
  • 2020-12-16 12:10

    You could create a generic tuple class for the purpose of returning multiple values. This seems to be a decent solution but I can't help but feel that you lose a bit of readability by returning such a generic type (Result is no better in that regard).

    One important point, though, that james curran also pointed out, is that the compiler enforces an assignment of the value. This is a general pattern I see in C#, that you must state certain things explicitly, for more readable code. Another example of this is the override keyword which you don't have in Java.

    0 讨论(0)
  • 2020-12-16 12:11

    There is a reason that one of the static code analysis (=FxCop) rules points at you when you use out parameters. I'd say: only use out when really needed in interop type scenarios. In all other cases, simply do not use out. But perhaps that's just me?

    0 讨论(0)
  • 2020-12-16 12:14

    Stay away from out. It's there as a low-level convenience. But at a high level, it's an anti-technique.

    int? i = Util.TryParseInt32("1");
    if(i == null)
        return;
    DoSomething(i);
    
    0 讨论(0)
  • 2020-12-16 12:20

    If you have even seen and worked with MS namespace System.Web.Security

    MembershipProvider 
       public abstract MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);
    

    You will need a bucket. This is an example of a class breaking many design paradigms. Awful!

    Just because the language has out parameters doesn't mean they should be used. eg goto

    The use of out Looks more like the Dev was either Lazy to create a type or wanted to try a language feature. Even the completely contrived MostCommonAnd42ndWord example above I would use List or a new type contrivedresult with 2 properties.

    The only good reasons i've seen in the explanations above was in interop scenarios when forced to. Assuming that is valid statement.

    0 讨论(0)
提交回复
热议问题