raymon raymon - 3 months ago 19
C# Question

Global Datatable filled in Private Function?

I discovered something strange in my code (C #) .
I could fill a data table from a public void in a private void .
As an example , I call my private function with the same name as the data table which is in the public void in the private function, in the private void, the datatable name is the same as it is in the Public section, this just filled in the public void without a return command in the private function, how is this possible?

My code:

public void start()
{
DataTable deliveryLines = new DataTable();
Orders(deliveryLines); // Private function

//And here, deliveryLines containts the information from the private void.
}

private void Orders(DataTable deliveryLines)
{
////Getting Filled blablabla
}

Answer

DataTable is a reference type. Variables of reference types store references to their data. If you pass such a variable to another method only the reference itself gets copied but not the object it points to.

In your code variable deliveryLines in method start() and argument variable deliveryLines in method Orders(DataTable) point to the same memory location.

See:

Reference Types (C# Reference)

Passing Reference-Type Parameters (C# Programming Guide)

Comments