MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

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

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>.

Upvotes: 0

Views: 306

Answers (3)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

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 appropriately 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 wrong usage of the keyword. However it assumes clients of our API read the docs carefully.

Upvotes: 0

eocron
eocron

Reputation: 7526

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.
}

Upvotes: -2

Patrick Hofman
Patrick Hofman

Reputation: 156978

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.

Upvotes: 5

Related Questions