这两种C#方法有什么区别

时间:2022-12-28 21:28:26

What is the difference between these two cases. Firstly, if I open the connection and pass it into my method as a parameter, compared to opening the connection directly in the method?

这两种情况有什么区别。首先,如果我打开连接并将其作为参数传递给我的方法,那么直接在方法中打开连接?

cnn.open()
func(cnn,param1,param2);

vs

func(cnn, param1,param2)
{
  cnn.open();
  //open connection here
}

5 个解决方案

#1


2  

There's no difference from the code you've posted other than in one case, your calling function needs to take care of opening/closing the connection, in the other, you'd expect the function to do it.

除了在一种情况下,你发布的代码没有区别,你的调用函数需要负责打开/关闭连接,另一方面,你期望函数执行它。

#2


0  

The difference is that in the second method, you open the connection.

不同之处在于,在第二种方法中,您打开连接。

In the first method you expect the method to only use a connection not caring about cleaning up the resources.

在第一种方法中,您希望该方法仅使用不关心清理资源的连接。

#3


0  

No functional difference but the lines for opening and closing a connection should usually be as close together as possible hence they should be in the same method.

没有功能差异,但打开和关闭连接的线应该尽可能地靠近在一起,因此它们应该采用相同的方法。

#4


0  

The difference is in how you want to use the connection and performance. If the function is a once off call and you are calling no other functions, or not doing anything else with the connection, then the second version of the function could even be reduced to:

不同之处在于您希望如何使用连接和性能。如果该函数是一次性调用并且您没有调用其他函数,或者没有对连接执行任何其他操作,那么该函数的第二个版本甚至可以简化为:

func(param1, param2) {
    Connection c = ....
    c.Open(...);
    ...
    c.Close();
}

However if you are calling many functions on the connection, even calling the function many times on the connection or if the creation and configuration of the connection is at a higher layer in your code, then you should use the first version of the function, with the addition of throwing an exception if the connection is not opened.

但是,如果在连接上调用许多函数,甚至在连接上多次调用函数,或者如果连接的创建和配置位于代码中的更高层,那么您应该使用函数的第一个版本,如果未打开连接,则添加抛出异常。

#5


0  

Well, I think you shouldn't ask for the different rather you should explain the situation you are in and ask for recommendation for what case must be used.

好吧,我认为你不应该问不同,而应该解释你所处的情况,并要求建议必须使用哪种情况。

Anyway, As Everybody told you In case 2 the connection object and its life cycle is encapsulated within the callee function. This is recommended if database operation out side this function is not desired.

无论如何,As Everybody告诉你在案例2中,连接对象及其生命周期被封装在被调用函数中。如果不需要数据库操作,则建议使用此功能。

Otherwise if you have any other database activity to be done out side this function scope like in caller function or any other function(other than func) being called from caller function then you should use the Case 1.

否则,如果您要在此函数范围内执行任何其他数据库活动,例如调用函数或从调用函数调用的任何其他函数(除了func),那么您应该使用案例1。

#1


2  

There's no difference from the code you've posted other than in one case, your calling function needs to take care of opening/closing the connection, in the other, you'd expect the function to do it.

除了在一种情况下,你发布的代码没有区别,你的调用函数需要负责打开/关闭连接,另一方面,你期望函数执行它。

#2


0  

The difference is that in the second method, you open the connection.

不同之处在于,在第二种方法中,您打开连接。

In the first method you expect the method to only use a connection not caring about cleaning up the resources.

在第一种方法中,您希望该方法仅使用不关心清理资源的连接。

#3


0  

No functional difference but the lines for opening and closing a connection should usually be as close together as possible hence they should be in the same method.

没有功能差异,但打开和关闭连接的线应该尽可能地靠近在一起,因此它们应该采用相同的方法。

#4


0  

The difference is in how you want to use the connection and performance. If the function is a once off call and you are calling no other functions, or not doing anything else with the connection, then the second version of the function could even be reduced to:

不同之处在于您希望如何使用连接和性能。如果该函数是一次性调用并且您没有调用其他函数,或者没有对连接执行任何其他操作,那么该函数的第二个版本甚至可以简化为:

func(param1, param2) {
    Connection c = ....
    c.Open(...);
    ...
    c.Close();
}

However if you are calling many functions on the connection, even calling the function many times on the connection or if the creation and configuration of the connection is at a higher layer in your code, then you should use the first version of the function, with the addition of throwing an exception if the connection is not opened.

但是,如果在连接上调用许多函数,甚至在连接上多次调用函数,或者如果连接的创建和配置位于代码中的更高层,那么您应该使用函数的第一个版本,如果未打开连接,则添加抛出异常。

#5


0  

Well, I think you shouldn't ask for the different rather you should explain the situation you are in and ask for recommendation for what case must be used.

好吧,我认为你不应该问不同,而应该解释你所处的情况,并要求建议必须使用哪种情况。

Anyway, As Everybody told you In case 2 the connection object and its life cycle is encapsulated within the callee function. This is recommended if database operation out side this function is not desired.

无论如何,As Everybody告诉你在案例2中,连接对象及其生命周期被封装在被调用函数中。如果不需要数据库操作,则建议使用此功能。

Otherwise if you have any other database activity to be done out side this function scope like in caller function or any other function(other than func) being called from caller function then you should use the Case 1.

否则,如果您要在此函数范围内执行任何其他数据库活动,例如调用函数或从调用函数调用的任何其他函数(除了func),那么您应该使用案例1。