I have an object 'connection' which holds the path/credentials to a SQL DB.
When calling methods; we usually do this:
Connection con = new Connection();
GetSalesData(con);
public static void (Connection con)
{
// Run code
}
As I understand, we just created two instance of connection, so that means two memory allocations. Is it better to do this:
Connection con = new Connection();
GetSalesData(ref con);
public static void (ref Connection con)
{
// Run code
}
Best Answer
No, we did not. Passing a variable automatically works as pass-by-reference for reference types and pass-by-value for value types. As your connection is a class, this means it is a reference type and only a reference will be passed to the method. You never created a second instance of your connection.
This has nothing to do with the static modifier, neither on the variable, nor on the method.
I suggest you spent a little while reading the MSDN on this topic, especially the two links called Passing Reference-Type Parameters and Passing Value-Type Parameters.
In addition, as another commenter remarked, you may want to read the excellent Jon Skeet on parameters.