Pass reference-type by reference to indicate it will be modified

依然范特西╮ 提交于 2019-12-12 04:26:47

问题


I found some code in our app that passes a List<T> by reference to indicate it is modified:

void DoSomething(ref List<MyType> theList)
{
    theList.Add(new MyType());
}

I think it is clear that the ref-keyword is obsolete in this case, as we could also add new elements to the list without the keyword. However it indicates that we modify the lists or at least its elements. I find this is particulary useful if you have many parameters and want to see which of them are modified and which are just passed as values to do the job.

The question if this is okay is surely opinion-based and would be invalid for SO, I rather ask if there´s another approach to achieve this or if I even should care about it.

EDIT: To clarify my question a bit. This question is not on if a list is modified, this was just an example. Alternatively I´d also use any other reference-type, not just a List<T>.


回答1:


However it indicates that we're changing the list.

No, it does not. It indicates the reference can be changed. Using a ref keyword as 'identifier' something can change is bad in my opinion since it opens doors you might not want.

I would advice to look into aspect-oriented programming, were you can assign attributes to method parameters. From the new Roslyn compiler with its code analysis services you could even check if the code violates the principal given.




回答2:


Patrick lead me to an alternative way that doesn´t rely on attributes so we can use it in legacy-code on machines with an older compiler. We can of course write the API-doc appropritely to indicate that the method modifies the passed parameter:

/// <summary/>
/// <param name="theList">list to be modified</param>
void DoSomething(List<MyType> theList)

I guess this is better as it doesn´t rely on a whrong usage of the keyword. However it assumes clients of our API read the docs carefully.




回答3:


It is much easier and broad to read interface than see ref and consider it changable collection.

For example, if you want it to be constant:

public DoSomething<T>(IReadOnlyCollection<T> collection)
{
     //....
}

Non-constant:

public DoSomething<T>(List<T> collection)
{
     //....
}

In case of value type it is indeed as you clarify in your question:

Constant:

public DoSomething(int value)
{
     //....
}

Non-constant:

public DoSomething(ref int value)
{
     //....
}

Or, if you have type without access to code, for example, Stream:

public class StreamWrapper
{
    private Stream _instance;

    //now you can specify read or edit methods here and use this class in invokation.
}


来源:https://stackoverflow.com/questions/41802039/pass-reference-type-by-reference-to-indicate-it-will-be-modified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!