none
Dataset - By value or By reference? RRS feed

  • Question

  •  

    Hi all

    I have a method that receives a Dataset as parameter.. This method returns a string that is the serialized version of the dataset (xml) including some CDATA Sections and other elements

    In my application, I have this call:

    return SerializeDataset(myDataset);

    Inside "SerilizeDataset" I only call the GetXml() method of the Dataset...

    My doubt is: is it better to pass the Dataset by value or by reference?

    Does .NET "copy" the Dataset to another area in the memory to use it within the method????

    Thanks!

    Tuesday, July 31, 2007 1:54 PM

Answers

  • Ok, I'm logged in under the right account this time. BugblatterII is actually me.

    I can see I'll have to prove what I said

    I created a little WinForm app containing the following code:

    Code Snippet

            private void button1_Click(object sender, EventArgs e)
            {
                DataSet dataset = new DataSet("TestDataset1");
                AddDataTableByRef(ref dataset);
                AddDataTableByVal(dataset);

                Console.WriteLine("DataSet name = " + dataset.DataSetName);
                Console.WriteLine("DataTable count = " + dataset.Tables.Count);
            }

            private void AddDataTableByRef(ref DataSet dataset)
            {
                dataset.Tables.Add(new DataTable("ByRefTable"));
                //dataset = new DataSet("TestDataset2");
            }

            private void AddDataTableByVal(DataSet dataset)
            {
                dataset.Tables.Add(new DataTable("ByValTable"));
                //dataset = new DataSet("TestDataset3");
            }


    The first method calls two identical methods in turn; identical that is but for the fact that one has the dataset passed in by ref and the other by val.

    On running this piece of code the output is:

    DataSet name = TestDataset1
    DataTable count = 2

    Each of the methods adds a single table to the dataset that's passed into it. This shows that both are adding a table to the original dataset, not a copy of the dataset.

    Now uncomment the two commented lines. The output is now:

    DataSet name = TestDataset2
    DataTable count = 1

    This illustrates the difference between passing by val and by ref.

    That line in the by ref method changes the reference within the method so that it now points to the new dataset. This therefore affects the reference in the first method, as it's the same reference.

    However the same line in the by val method has no effect. This is because the reference that's being set to a new dataset is actually a copy of the reference that was passed in.

    So the upshot is that whether you pass it in by val or by ref it's the same dataset, not a copy, and so the performance is the same. The only difference is that when passing by ref, if you change what the reference points to it'll affect the calling method. You're not doing that, so there's really no difference for you.

    Regards,

    Sean
    Tuesday, July 31, 2007 8:06 PM

All replies

  • It depends upon what you mean by "better". Assuming you understand the difference, in once instance you receive a copy of the data (ByVal) and in the other you receive a reference pointer to the orginal data (ByRef). In most intances there is litte performance difference between the two methods so you can choose the method which best satisfies your implementation.

    If you don't need to return changed data back to the calling routine then pass it ByVal. If you need to change the data before returning it to the calling routine then pass it ByRef.

    Tuesday, July 31, 2007 2:11 PM
  • Here's my understanding of how this works.

    When you pass a reference type (e.g. a DataSet) by ref the original reference is passed in.

    When you pass a reference type by val a copy of the original reference is passed in. However both the original reference and the copy point to the same object. The object is not copied, just the reference to it.

    So for your purpose there's virtually no difference, as the time taken to make a copy of a reference is negligible.

    Regards,

    Sean
    Tuesday, July 31, 2007 2:26 PM
  •  Paul P Clement IV wrote:

    It depends upon what you mean by "better". Assuming you understand the difference, in once instance you receive a copy of the data (ByVal) and in the other you receive a reference pointer to the orginal data (ByRef). In most intances there is litte performance difference between the two methods so you can choose the method which best satisfies your implementation.

    If you don't need to return changed data back to the calling routine then pass it ByVal. If you need to change the data before returning it to the calling routine then pass it ByRef.

     

    What do you mean by "return changed data back to the calling routine"?

     

    I only call GetXml() within the method... I don't change the data, just read it... It's better to pass it ByVal so, right?

    Tuesday, July 31, 2007 2:37 PM
  • Essentially it means that if you pass ByRef then any data in the structure you change will be reflected in the calling routine.

     

    Yes, if you don't need to change the data then pass it ByVal.

    Tuesday, July 31, 2007 3:02 PM
  •  Paul P Clement IV wrote:

    Essentially it means that if you pass ByRef then any data in the structure you change will be reflected in the calling routine.

     

    Yes, if you don't need to change the data then pass it ByVal.

     

    Right. This I was aware of.

    But what is the performance impact?

    Tuesday, July 31, 2007 6:39 PM
  • Probably very little performance impact. But then it will probably depend upon how much data you're working with. The only way to know for certain is to test it out.

     

    Tuesday, July 31, 2007 6:53 PM
  • Ok, I'm logged in under the right account this time. BugblatterII is actually me.

    I can see I'll have to prove what I said

    I created a little WinForm app containing the following code:

    Code Snippet

            private void button1_Click(object sender, EventArgs e)
            {
                DataSet dataset = new DataSet("TestDataset1");
                AddDataTableByRef(ref dataset);
                AddDataTableByVal(dataset);

                Console.WriteLine("DataSet name = " + dataset.DataSetName);
                Console.WriteLine("DataTable count = " + dataset.Tables.Count);
            }

            private void AddDataTableByRef(ref DataSet dataset)
            {
                dataset.Tables.Add(new DataTable("ByRefTable"));
                //dataset = new DataSet("TestDataset2");
            }

            private void AddDataTableByVal(DataSet dataset)
            {
                dataset.Tables.Add(new DataTable("ByValTable"));
                //dataset = new DataSet("TestDataset3");
            }


    The first method calls two identical methods in turn; identical that is but for the fact that one has the dataset passed in by ref and the other by val.

    On running this piece of code the output is:

    DataSet name = TestDataset1
    DataTable count = 2

    Each of the methods adds a single table to the dataset that's passed into it. This shows that both are adding a table to the original dataset, not a copy of the dataset.

    Now uncomment the two commented lines. The output is now:

    DataSet name = TestDataset2
    DataTable count = 1

    This illustrates the difference between passing by val and by ref.

    That line in the by ref method changes the reference within the method so that it now points to the new dataset. This therefore affects the reference in the first method, as it's the same reference.

    However the same line in the by val method has no effect. This is because the reference that's being set to a new dataset is actually a copy of the reference that was passed in.

    So the upshot is that whether you pass it in by val or by ref it's the same dataset, not a copy, and so the performance is the same. The only difference is that when passing by ref, if you change what the reference points to it'll affect the calling method. You're not doing that, so there's really no difference for you.

    Regards,

    Sean
    Tuesday, July 31, 2007 8:06 PM
  • Hmmm Sean..w.

    Thanks for your explanation!! ;-)

    Wednesday, August 1, 2007 12:33 PM