我知道很多例子都存在,其中定义了SqlConnection,然后在Using块中定义了SqlCommand:
using (var conn = new SqlConnection(connString)) {
using (var cmd = new SqlCommand()) {
cmd.Connection = conn;
//open the connection
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:如果我直接在SqlCommand上定义连接,那么当命令被释放时连接是否会关闭?
using (var cmd = new SqlCommand()) {
cmd.Connection = new SqlConnection(connString);
//open the connection
}
Run Code Online (Sandbox Code Playgroud) 在C++中,您通常可以通过仔细使用"using"关键字来大幅提高代码的可读性,例如:
void foo()
{
std::vector< std::map <int, std::string> > crazyVector;
std::cout << crazyVector[0].begin()->first;
}
Run Code Online (Sandbox Code Playgroud)
变
void foo()
{
using namespace std; // limited in scope to foo
vector< map <int, string> > crazyVector;
cout << crazyVector[0].begin()->first;
}
Run Code Online (Sandbox Code Playgroud)
python是否存在类似的东西,或者我是否必须完全限定所有内容?
我将添加免责声明,我知道使用有其缺陷,应该适当限制范围.
有这个代码:
using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create)))
{
//save something here
}
Run Code Online (Sandbox Code Playgroud)
我们需要关闭BinaryWriter吗?如果没有,为什么?
我正在尝试打开一个(实际上有数百个)excel文件.我打开应用程序但想要在我打开的每个工作簿周围使用Using()功能.为什么会导致错误?
using (Excel.Workbook wbXL = appXL.Workbooks.Open(_sourceFullPath, Type.Missing, Excel.XlFileAccess.xlReadOnly))
{
//stuff with wbXL
}
Run Code Online (Sandbox Code Playgroud)
使用获取红色下划线并说"'Microsoft.Office.Interop.excel.Workbook':在using语句中使用的类型必须可以隐式转换为'System.IDisposable'.
如何使这项工作?
我已经在USING语句中包装我的OracleConnection和OracleCommand对象一段时间了,但是,在运行代码分析器之后,我发现OracleParameter也实现了IDisposable.以下代码是否正确?是否有更好的可读性或结构技术?乍一看,它似乎与USING语句混杂在一起:
using (OracleConnection conn = new OracleConnection(connectionstring))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
cmd.BindByName = true;
using (OracleParameter param1 = new OracleParameter("p1", OracleDbType.Int32, System.Data.ParameterDirection.Input))
{
param1.Value = int.Parse(value1);
cmd.Parameters.Add(param1);
}
using (OracleParameter param2 = new OracleParameter("p2", OracleDbType.Varchar2, System.Data.ParameterDirection.Input))
{
param2.Value = value2;
cmd.Parameters.Add(param2);
}
using (OracleDataReader dr = cmd.ExecuteReader())
{
// loop data here...
}
}
}
Run Code Online (Sandbox Code Playgroud) 在我写的石头,剪刀,剪刀程序中,我列举了三种不同的动作并将它们声明为一个类。但是,当我尝试编写using语句以便避免使用范围运算符时,它似乎不起作用。有人知道为什么吗?
enum class choice {rock, paper, scissors};
using namespace choice;
出现一条错误消息,说:[错误]'选择'不是名称空间名称。为什么是这样?我认为在这种情况下可以选择一个名称空间。
我无法删除IE9中的#符号.我搜索了一个答案,但没有找到解决办法.
这总是重定向到
http://myhost.com:8080/#/website/
Run Code Online (Sandbox Code Playgroud)
并显示此描述:
The requested resource is not available.
Run Code Online (Sandbox Code Playgroud)
locationprovider.html5mode(true)不管用.同样的路线在FireFox中工作并显示
http://myhost.com:8080/website/
Run Code Online (Sandbox Code Playgroud)
我怎么能纠正这个?
System.Windows.Markup.XamlParseExceptionoccurred
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '21' and line position '6'.
"Could not load file or assembly 'ResourceLibrary, Culture=neutral' or one of its dependencies. The system cannot find the file specified.":"ResourceLibrary, Culture=neutral"
=== Pre-bind state information ===
LOG: DisplayName = ResourceLibrary, Culture=neutral
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: ResourceLibrary, Culture=neutral | Domain ID: 2
WRN: A partial bind …Run Code Online (Sandbox Code Playgroud) 如果基类依赖于模板参数,则不会在非限定名称查找中检查其范围。我可以使用该using声明从基类中引入名称。假设现在我在基类中有一个类型别名模板。可以使用该using声明将其引入派生类吗?
template<class T>
struct Base
{
using Type1 = int;
template<typename S>
using Type2 = S;
};
template<class T>
struct Derived : Base<T>
{
using typename Base<T>::Type1; // Fine
//using Type1 = typename Base<T>::Type1; // Also fine
template<typename S>
using Type2 = typename Base<T>::template Type2<S>;
};
Run Code Online (Sandbox Code Playgroud)
可以Type2用类似于(未注释)的行替换行Type1吗?
假设我有一个方法foo可以返回ValueTuple其中一个成员是一次性的,例如(IDisposable, int).
确保返回的一次性物体在呼叫方面正确配置的最佳方法是什么?
我尝试了以下方法:
using (var (disposable, number) = foo())
{
// do some stuff using disposable and number
}
Run Code Online (Sandbox Code Playgroud)
但这不会编译:
'(IDisposable disposable,int number)':using语句中使用的类型必须可以隐式转换为'System.IDisposable'
我是否真的需要将我的代码包装在try-finally块中并将我的对象显式地放在finally块中,或者有更好的方法吗?
实际上我希望看到新的ValueTuples实现IDisposable接口并调用Dispose()他们所有的一次性成员.这可能是微软有价值的功能要求吗?
using ×10
c# ×6
idisposable ×3
c++ ×2
namespaces ×2
angularjs ×1
c++11 ×1
class ×1
coding-style ×1
enumeration ×1
excel ×1
interop ×1
python ×1
routes ×1
templates ×1
valuetuple ×1
wpf ×1