Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Pls some one tell that why we use ref in method parameter
code is
C#
private static void closeConnection(ref SqlConnection objCon)
        {
            try
            {
                if (objCon.State == ConnectionState.Open)
                {
             //objCon.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectDB"].ToString();
             objCon.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

i want to know why we take ref Sqlconnection objcon in method
pls some one tell.....
Posted
Updated 24-Sep-12 21:18pm
v3

Without the ref keyword the method will still function the same. Only when the objCon will reference another SqlConnection instance the use of ref keyword will be needed.

If you would like to Dispose the objCon and set it to null, then you would need the ref keyword. In that case in the calling method the instance of object would also be null. (But I guess then your method should be called closeAndDisposeConnection).
 
Share this answer
 
i think in concurrent thread if afraid if the values passed to function calls may overlap for simultaneous calls.

Regard
Sham
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900