The DataSet class is a reference class and therefore gets treated differently than value types (i.e. ints, doubles, etc).
There are two ways in which you can pass a reference type into a function: 1) passing reference types by value 2) passing reference types
If you pass a reference type by value, you are simply saying that any changes you make to reassign the reference inside the method will not change the reference outside the function.
If you pass a reference type by reference (using the ref keyword) you are saying that you can reassign the pointer (or reference) to the object inside the method and have it persist outside the function.
More information can be found here: http://msdn.microsoft.com/library/default.asp url=/library/en-us/csref/html/vclrfPassingMethodParameters.asp
The best example is on the MSDN website: // PassingParams6.cs
using System;
class SwappinStrings
{
static void SwapStrings(ref string s1, ref string s2)
// The string parameter x is passed by reference.
// Any changes on parameters will affect the original variables.
{
string temp = s1;
s1 = s2;
s2 = temp;
Console.WriteLine("Inside the method: {0}, {1}", s1, s2);
}
public static void Main()
{
string str1 = "John";
string str2 = "Smith";
Console.WriteLine("Inside Main, before swapping: {0} {1}",
str1, str2);
SwapStrings(ref str1, ref str2); // Passing strings by reference
Console.WriteLine("Inside Main, after swapping: {0}, {1}",
str1, str2);
}
}OutputInside Main, before swapping: John Smith
Inside the method: Smith, John
Inside Main, after swapping: Smith, John Code DiscussionIn this example, the parameters need to be passed by reference to affect the variables in the calling program. If you remove the ref keyword from both the method header and the method call, no changes will take place in the calling program.
|