为什么我们在C#中将静态方法与ref SqlConnection objConn一起使用 [英] why we use static method with ref SqlConnection objConn in c#

查看:82
本文介绍了为什么我们在C#中将静态方法与ref SqlConnection objConn一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人告诉我们为什么在方法参数
中使用ref 代码是

Pls some one tell that why we use ref in method parameter
code is

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);
            }
        }


我想知道为什么我们在方法
中使用ref Sqlconnection objcon 请有人告诉.....


i want to know why we take ref Sqlconnection objcon in method
pls some one tell.....

推荐答案

如果没有ref关键字,该方法仍将起作用.仅当objCon将引用另一个SqlConnection实例时,才需要使用ref关键字.

如果您想处置objCon并将其设置为null,则需要ref关键字.在这种情况下,在调用方法中,object的实例也将为null. (但是我想那您的方法应该叫做closeAndDisposeConnection.)
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).


我想在并发线程中,如果担心传递给函数调用的值可能会在同时调用中重叠.
问候
i think in concurrent thread if afraid if the values passed to function calls may overlap for simultaneous calls.

Regard
Sham


您的答案在这里

http://stackoverflow. com/questions/635915/when-to-use-ref-and-when-it-is-not-necessary-in-c-sharp [ ^ ]

谢谢
--RA
Your Answer is here

http://stackoverflow.com/questions/635915/when-to-use-ref-and-when-it-is-not-necessary-in-c-sharp[^]

Thanks
--RA


这篇关于为什么我们在C#中将静态方法与ref SqlConnection objConn一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆