1

I am doing this,

public Order Add(Order order)
{
    order.thisA = GetValue1();
    // update the state of object
    return order;
}

Would i be any beneficial if I use Ref or Out here instead ?

Further implementation would require me to add this method,

public Order[] UpdateCollection(Order[] orderCollection)
{
    foreach(Order o in orderCollection)
          o = Update(o);

    return orderCollection;
}

Please tell me in light of best practices as well.

Context:

At the moment I am only returning INT id of the order but in future maybe I need to return more properties, but here is the full context,

https://codereview.stackexchange.com/questions/97778/crud-operation-class#

Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • 1
    Without any context it is impossible to say, also as a favour to your fellow programmer, please don't call your object `object`. the code highlighting shows why – Sayse Jul 24 '15 at 07:22
  • Short answer is no, you want to update the properties of 'object', not the reference to it – vc 74 Jul 24 '15 at 07:22
  • @Sayse I added context now with some code – Mathematics Jul 24 '15 at 07:24
  • take a look at this Hopefully it should answer your question http://stackoverflow.com/questions/8209476/best-practice-ref-parameter-or-return-value – bilpor Jul 24 '15 at 07:25
  • I've removed my downvote but your update hasn't made it much clearer, you might want to explain how your question relates to it. Even then it may become opinion based but possibly not. Is it safe to assume that `MyObject` is a value type? – Sayse Jul 24 '15 at 07:32
  • @Sayse please see edit, it's just a class with properties – Mathematics Jul 24 '15 at 07:38

4 Answers4

5

In this case:

public MyObject Update(MyObject object)
{
    object.thisA = GetValue1();
    // update state of object
    return object;
}

You are not changing MyObjects reference so:

  1. You don't need to return the object back.
  2. You don't need to use ref.
  3. You don't need to use out.

Using out is for initializing an object (You must assign a value in the function).

MyObject obj; // didn't assign anything
Method(out obj);

public void Method(out MyObject obj){
   obj = new MyObject(); // assigned
}

using ref is in case you might change the reference of the object inside the method:

MyObject obj = new MyObject();
Update(ref obj);

public void Update(ref MyObject obj)
{
    obj = new MyObject(); // changing the ref!!!
    obj.thisA = GetValue1();
}

BTW, in your case you don't need to do anything:

public void UpdateCollection(Order[] orderCollection)
{
    foreach(Order o in orderCollection)
          Update(o);
}

public void Update(MyObject object)
{
    object.thisA = GetValue1();
}

You are foreaching an array and updating the object and not the reference.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • 1
    You typed out my answer pretty much, just a little faster. Upvoted! If I should be honest though, his issue is that he doesn't understand the difference between the three definitions. – Bauss Jul 24 '15 at 07:38
  • @Amir would you answer be valid with collection method as well ? – Mathematics Jul 24 '15 at 07:42
  • Yes. A collection is a reference type like MyObject and therefore if you want to change the reference inside a method you can either return the new object or use ref. – Amir Popovich Jul 24 '15 at 07:43
  • @Mathematics - I've edited and added your example. – Amir Popovich Jul 24 '15 at 07:46
2

You use ref if there is a chance that you want to change the reference instead of the object's data.

Example:

public void RetargetReference(ref List<string> originalList)
{
    originalList = new List<string>();
    originalList.Add("World");
}

List<string> inList = new List<string>();
inList.Add("Hello");
RetargetReference(ref inList);

This will change the reference of inList. It will now point to a new list which contains one entry "World". The list that contained "Hello" will no longer be available to you unless you have another reference to it.

ref parameters can be useful if you want to change parameter you passed in during the execution of the method.

out will be used to have the method create a new object instance without you being able to pass a value in!

Example:

public void CreateReference(out List<string> newList)
{
    newList = new List<string>();
    newList.Add("Hello World");
}

List<string> list;
CreateReference(out list);

After that, list will point to a new List<string> instance. Inside the method you have no access to whatever newList actually "points to". You will always have to create a new instance.

out parameters can be useful if you want your method to return more than one result. For example, the following method would return a bool to indicate success and two out parameters that contain the data:

public bool TrySplitString(string source, out string part1, out string part2)
{
    part1 = String.Empty;
    part2 = String.Empty;

    string[] parts = source.Split('=');
    if (parts.Length != 2)
        return false;

    part1 = parts[0];
    part2 = parts[1];
    return true;
}

Objects are generally passed by reference in C#, so the following method actually changes the data "outside" the method:

public void ChangeList(List<string> list)
{
   list.Add("World");
}

List<string> inList = new List<string>();
inList.Add("Hello");
ChangeList(inList);

After that, inList contains two entries: "Hello" and "World".

The reason why sometimes you return the object that was passed in as a parameter is that this allows so called "method chaining" where you can "write sentences" instead of "commands". You'll see that in my next example:

public static List<string> CreateList()
{
    return new List<string>();
}

public static List<string> AddItem(this List<string> list, string item)
{
    list.Add(item);
    return list;
}

public static List<string> DoSomethingWithList(this List<string> list)
{
    ...;

    return list;
}

You can use this code and write something like this:

List<string> list = CreateList().AddItem("Hello").DoSomethingWithList();
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

Return object back may be used for create "method chaining", like this:

someObject.Update(MyObject object).MyObjectMethod1().MyObjectMethod2();
Dmitry
  • 512
  • 7
  • 15
0

I can't really workout what you really trying to achieve. However, i will give you some basics.

In C#, arguments can be passed to parameters either by value or by reference.

If your MyObject is a reference type (eg: class) then it is passed to function as reference and hence you will end up in modifying same object. In other way, any change you make on this instance (within the function) will have a direct impact on that instance and it will continue to be true in the current execution environment.

On the other hand, if MyObject is a value type (eg: struct) then a copy of it will be passed to method. That is, original instance of your value type is not altered even if you modify the members within the method. This is default behavior when it comes to value type.

Now assume a scenario where you wanted to modify the original instance of value type. This can be achieved only if you pass the reference of your value type. Here comes ref and out. Using these keyword, you can pass parameter by reference. There are differences between ref and out, which can be learned separately. However, keeping your context in mind, the ref and out both allow the called method to modify a parameter. ref means that the parameter has a value on it before going into the function. So that the function can read and or change the value within it. On the other hand, out means that the parameter has no official value before going into the function. The called function must initialize it before parameter goes out from function.

S.N
  • 4,910
  • 5
  • 31
  • 51