What's so bad about ref parameters?

后端 未结 12 601
隐瞒了意图╮
隐瞒了意图╮ 2021-01-11 09:49

I\'m faced with a situation that I think can only be solved by using a ref parameter. However, this will mean changing a method to always accept a ref parameter when I only

相关标签:
12条回答
  • 2021-01-11 10:09

    ref is just a tool. You should think: What is the best design pattern for what I am building?

    • Sometimes will be better to use an overloaded method.

    • Others will be better to return a custom type or a tuple.

    • Others will be better to use a global variable.

    • And others ref will be the right decision.

    0 讨论(0)
  • 2021-01-11 10:10

    The most common use I've seen for ref parameters is as a way of returning multiple values. If that's the case, you should consider creating a class or struct that returns all the values as one object. If you still want to use a ref but want to make it optional, add a function overload.

    0 讨论(0)
  • 2021-01-11 10:11

    This is one of those things that F# or other functional programming languages solve a lot better with returning Tuple values. Its a lot more cleaner and terse syntax. In the book I am reading on F#, it actually points out the C# equivelant of using ref as doing the same thing in C# to return multiple parameters.

    I have no idea if it is a bad practice or there is some underlying "booga booga" about ref parameters, to me they just feel as not clean syntax.

    0 讨论(0)
  • 2021-01-11 10:13

    If your method only needs this ref parameter 5% of the time perhaps you need to break this method down. Of course without more details its hard to say but this to me smells like a case of violating single responsability principal. Perhaps overloading it will help.

    As for your question there is no issue in my opinion passing a parameter as a reference although it is not a common thing to run into.

    0 讨论(0)
  • 2021-01-11 10:19

    An overload won't kill your application or its design. As long as the intent is clearly documented, it should be okay.

    One thing that might be considered is mitigating your fears about the ref parameter through a different type of parameter. For example, consider this:

    public class SaveArgs
    {
       public SaveArgs(TEntity value) { this.Value = value; }
    
       public TEntity Value { get; private set;}
       public int NewId { get; internal set; }
       public bool NewIdGenerated { get; internal set; } 
    }
    

    In your code, you simply pass a SaveArgs rather than the TEntity, so that you can modify its properties with more meaningful information. (Naturally, it'd be a better-designed class than what I have above.) But then you wouldn't have to worry about vague method interfaces, and you could return as much data as you needed to in a verbose class.

    Just a thought.

    EDIT: Fixed the code. My bad.

    0 讨论(0)
  • 2021-01-11 10:27

    Can you add an overload? Have one signature without the ref parameter, and one with it.

    Ref parameters can be useful, and I'm glad they exist in C#, but they shouldn't be used without thought. Often if a method is effectively returning two values, it would be better either to split the method into two parts, or encapsulate both values in a single type. Neither of these covers every case though - there are definitely times when ref is the best option.

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