Hello everyone,
In my sample below, abcd is out parameter. I think it only reduces the value copy from input parameter of caller (Main), and it does not reduces value copy from instance returned from DateTime.Now to abcd (assignment), correct?
(in more details when we do assignment abcd = DateTime.Now, a new instance will be created, and value copied from DateTime.Now return instance, and making abcd point to the new instance?)
[Code]
static void Test2(out DateTime abcd)
{
abcd = DateTime.Now;
}
static void Main(string[] args)
{
DateTime abcd;
Test2(out abcd);
return;
}
[/Code]
thanks in advance,
George
George GeorgePosted Jun 16, 2008, 5:47 AM
Thanks Dave,
Question answered.
regards,
George
DavePosted Jun 16, 2008, 5:05 AM
out basically allows you to pass a variable, which has been declared but not yet initialised, to a function . In my example, the variable is initialised and populated in the NewPerson function. No need to return a value, as the object is initialised in the memory location of the out variable. So the return value is void.
George GeorgePosted Jun 16, 2008, 2:51 AM
Thanks Dave,
I like your sample, which shows there is no need to fill in values for out variable -- it is the responsibility of the called function to fill the values. Correct? :-)
regards,
George
DavePosted Jun 16, 2008, 2:31 AM
I think you are probably right George. Unlike C++ (which I am currently teaching myself), in C#, we don't have visibility of how the DateTime structure overloads the assignment operator. That being the case, we need to resort to the intermediate language to see what is really going on. So, there is probably a temporary DateTime instance created, which is then memberwise copied to the memory location of abcd.
The little program that I wrote a couple of years back to remember how the out keyword works follows:
class Program
{
static void Main(string[] args)
{
Person[] pArray = new Person[3];
Person aGuy;
NewPerson(out aGuy);
pArray[0] = aGuy;
NewPerson(out aGuy);
pArray[1] = aGuy;
NewPerson(out aGuy);
pArray[2] = aGuy;
foreach (Person p in pArray)
Console.WriteLine(p);
}
private static void NewPerson(out Person aGuy)
{
int age;
string name;
Console.Write("Enter the guy's age: ");
age = int.Parse(Console.ReadLine());
Console.Write("Enter the guy's name: ");
name = Console.ReadLine();
aGuy = new Person(age, name);
}
class Person
{
private int age;
private string name;
public Person()
{ }
public Person(int age, string name)
{
this.age = age;
this.name = name;
}
public int Age
{ get { return age; } set { age = value; } }
public string Name
{ get { return name; } set { name = value; } }
public override string ToString()
{
return String.Format("Name: {0}\nAge: {1}", age, name);
}
}
Perhaps you will find it useful.
George GeorgePosted Jun 15, 2008, 11:37 PM
Thanks Fernando,
After some further study, I do not agree with you. :-)
Here is the IL code, you can see DateTime.Now will create a new instance and put it on evaluation stack, then stobj [mscorlib]System.DateTime statement will create another new instance and load the instance to variable abcd. Any comments? Please feel free to correct me if I am wrong.
.method private hidebysig static void Test2([out] valuetype [mscorlib]System.DateTime& abcd) cil managed
{
.maxstack 8
L_0000: nop
L_0001: ldarg.0
L_0002: call valuetype [mscorlib]System.DateTime [mscorlib]System.DateTime::get_Now()
L_0007: stobj [mscorlib]System.DateTime
L_000c: ret
}
regards,
George
Fernando SotoPosted Jun 15, 2008, 11:32 PM
To your question, "in more details when we do assignment abcd = DateTime.Now, a new instance will be created, and value copied from DateTime.Now return instance, and making abcd point to the new instance?)"
When you do this, abcd = DateTime.Now, in the Test2 method, you are creating a new instance of the object of DateTime but assigning it to the same memory location pointed to by abcd. There is no copying or returning a value to the caller it writes it to the same memory location.
private void button1_Click(object sender, EventArgs e)
{
// This creates a variable of type DataTime and initializes the variable
// to 1/1/0001 12:00 AM
DateTime abcd;
// The call to Test2, the parameter abcd is used as a call by reference
Test2(out abcd);
}
static void Test2(out DateTime abcd)
{
// The variable abcd in this method points to the same variable
// memory location that the caller used so you are initializeing the DateTime
// of the caller here and not creating a new varable.
abcd = DateTime.Now;
}
Fernando