有没有人知道using从类或整个解决方案中删除冗余语句的工具?
我正在使用Refactor!addin具有"移动类型到单独文件"的智能标记,但是它using使用原始类中的所有子句.
我想知道如何使用语句处理异常?我是否需要使用Try/Cath/Finally子句包装using语句,以确保即使包含的代码抛出异常,SqlConnection对象也会被关闭和处理?
Public Function GetUserAccountKeyByUsername(ByVal pUsername As String) As Int32
If String.IsNullOrEmpty(pUsername) Then
Throw New ArgumentNullException("pUsername", "Username is missing")
End If
Dim o As Object
Dim userAccountKey As Int32
Dim SQL As StringBuilder = New StringBuilder()
With SQL
.Append("SELECT USER_KEY ")
.Append("FROM USER ")
.Append("WHERE USERNAME = @Username ")
End With
Try
Using conn As SqlConnection = New SqlConnection(ConnectionString)
conn.Open()
Using cmd As SqlCommand = New SqlCommand(SQL.ToString, conn)
Try
cmd.CommandTimeout = Convert.ToInt32(ConfigurationManager.AppSettings("SQLQueryLimitTime"))
cmd.Parameters.Add(New SqlParameter("@Username", SqlDbType.VarChar)).Value = pUsername
o = …Run Code Online (Sandbox Code Playgroud) 我读过这样的用法:
using (myObject)
{
myObject.DoStuff();
}
Run Code Online (Sandbox Code Playgroud)
可以这样想:
try
{
myObject.DoStuff();
}
finally
{
myobject.Dispose()
}
Run Code Online (Sandbox Code Playgroud)
因此,如果myObejct.DoStuff抛出ExceptionA然后myObject.Dispose()也抛出异常(ExceptionB),那么ExceptionA将丢失.(有关更好的说明,请参阅此处的MSDN示例.)
这是否意味着如果使用块代码中的代码可能抛出异常(大多数代码是正确的?)那么using语句是不好的做法?
我有这个(仅限插图)C#代码:
using( new System.IO.MemoryStream() ) {
System.Threading.Thread.Sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)
请注意,此处MemoryStream创建a 并未明确绑定到引用.因此,除非由于using语句而有一些特殊处理,否则对象没有引用它,并且可以在控制离开using语句之前收集,甚至可能在Sleep()完成之前收集.
MemoryStream在控制权离开using声明之前是否有资格收集?
我在学校学习了C#,现在我开始学习Java了.
在Java中,有"尝试使用ressources",当它不再使用时会关闭/处理东西(如扫描仪).
等效的C#是using-Statement,它基本上是一样的.
它们是真的完全相同,还是有任何差异(比如他们在后台做的事情)?
我想使用using语句,但如果它应该指向的对象不存在,则可能需要更改我“使用”的变量的值。
我想到了这样的事情(用于注册表访问和 32/64 窗口 - 尽管这是我当前的用例,但这是一个普遍问题):
using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MS\Platform"))
{
if (key == null)
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\MS\Platform");
// use key
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不能编译:
error CS1656: Cannot assign to 'key' because it is a 'using variable'
Run Code Online (Sandbox Code Playgroud)
我可以通过不使用using而是 try/catch/finally 和/或在使用之前测试注册表项是否存在来解决此问题。
有没有办法继续使用using,然后处理正确的对象?
我一直在重写一些旧代码,以便该using语句用于我的DataTables,而不是Dispose每次都记住:
using (DataTable dt = BLL.GetDataTable()) {
foreach(DataRow dr in dt.Rows) {
// iteration logic
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在一种特定情况下,DataTable的内容基于变量而有所不同,因此我创建了初始DataTable,然后再分配值:
DataTable dt = new DataTable();
switch(foo) {
case bar:
dt = BLL.GetDataTable(bar);
break;
default:
dt = BLL.GetDataTable();
break;
}
// iteration logic here
dt.Dispose();
Run Code Online (Sandbox Code Playgroud)
更改为使用using,我有:
using (DataTable dt = new DataTable()) {
switch(foo) {
case bar:
dt = BLL.GetDataTable(bar);
break;
default:
dt = BLL.GetDataTable();
break;
}
// iteration logic here
}
Run Code Online (Sandbox Code Playgroud)
那是好的做法(即empty使用using语句创建DataTable …
I'm wondering if the use of object initializers inside using statements somehow prevents the correct disposal of the resource declared inside them, e.g.
using (Disposable resource = new Disposable() { Property = property })
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
I've read that object initializers are nothing but synctatic sugar, which the compiler translates to something similar to the following code:
MyClass tmp = new MyClass();
tmp.Property1 = 1;
tmp.Property2 = 2;
actualObjectYouWantToInitialize = tmp;
Run Code Online (Sandbox Code Playgroud)
Even if I may appear as a …
我有以下代码:
using (SqlCommand command = new SqlCommand())
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Connection = new SqlConnection();
command.CommandText = "";
command.Parameters.Add(new SqlParameter("@ExperienceLevel", 3).Direction = System.Data.ParameterDirection.Input);
SqlDataReader dataReader = command.ExecuteReader();
}
Run Code Online (Sandbox Code Playgroud)
在声明SqlConnection的地方是否有任何功能影响我目前正在声明它而不是这样?:
using (SqlCommand command = new SqlCommand())
using (SqlConnection connection = new SqlConnection())
Run Code Online (Sandbox Code Playgroud)
谢谢
我在使用声明方面所做的大部分研究,包括阅读各种样式指南的相关部分,表明在C++源文件中使用声明是否使用,只要它们出现在所有#includes之后,就是决定留给编码员.即使是我阅读过的风格指南,为了保持一致性,这些风格指南通常也会出现在一方或另一方面,但在这方面相当灵活.
我的问题是,鉴于这种高度的灵活性,使用一致的风格有多重要?例如,假设作者写了类似的东西
using std::vector;
vector<T> v;
std::cout << v[0] << std::endl;
Run Code Online (Sandbox Code Playgroud)
是否在std :: vector上使用不一致的应用程序而不是std :: cout或std :: endl通常被认为是可接受的,还是被认为是无纪律的?
using-statement ×10
c# ×8
.net ×2
ado.net ×1
c++ ×1
dispose ×1
idisposable ×1
java ×1
namespaces ×1
using ×1
vb.net ×1