说我有这两个对象:
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(sql, connection);
Run Code Online (Sandbox Code Playgroud)
要关闭连接或Oracle,我是否必须调用command.Dispose(),connection.Dispose()或两者?
这够好了:
using(connection)
{
OracleDataReader reader = cmd.ExecuteReader();
// whatever...
}
Run Code Online (Sandbox Code Playgroud) 在VB.Net中你可以做如下的事情,没有任何问题...只是忽略这是一个非常无用的类的事实:-)
Imports System
Public Class Class1
Public Shared Function ArrayToList(ByVal _array() As String) As Collections.Generic.List(Of String)
Return New Collections.Generic.List(Of String)(_array)
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
但是如果你在C#中做同样的事情......
using System;
public class Class1
{
public static Collections.Generic.List ArrayToList(string[] _array)
{
return new Collections.Generic.List(_array);
}
}
Run Code Online (Sandbox Code Playgroud)
您将在"Collections.Generic.List"返回的行上收到错误,说"找不到类型或命名空间名称'Collections'(您是否缺少using指令或程序集引用?)"
我知道你必须有一个使用System.Collections.Generic的using指令才能使用List,但我不知道为什么.我也不明白为什么我在函数声明中没有得到相同的错误,只是在return语句中.
我希望有人可以解释这一点,甚至可以将我推荐给一个解释它的technet页面.我四处寻找,但找不到任何解释这个概念的东西.
编辑:请注意,问题实际上是关于子命名空间的引用,例如在示例中能够引用系统中的集合.
对于使用cout,我需要指定两者:
#include<iostream>
Run Code Online (Sandbox Code Playgroud)
和
using namespace std;
Run Code Online (Sandbox Code Playgroud)
在哪里cout定义?在iostream,对吗?那么,它iostream本身就是命名空间std吗?
关于使用的两个陈述的含义是什么cout?
我很困惑为什么我们需要将它们都包括在内.
我刚刚开始使用resharper,它在这个生成的assemblyinfo.cs文件中咆哮.
Reason/Logic告诉我,我可以把它排除在外,因为它是resharper的最新版本,它是一个流行的工具.
我的担心与我的有限理解有关,这与新的c#特征的相互作用,例如扩展方法结合使用似乎没有"使用"的"使用".
希望这有点道理.
这是为了清晰起见屏幕截图.

这是我的复选框HTML代码
<input id="termsCheckbox" name="termsCheckbox" type="checkbox" value="terms" <?PHP echo $terms; ?> class="checkbox">
Run Code Online (Sandbox Code Playgroud)
这是javascript代码
var terms = $("#termsCheckbox");
function validateTerms(){
if(termsCheckbox.checked == false){
terms_div.addClass("terms_error");
return false;
}
else{
terms_div.removeClass("terms_error");
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我想检查是否选中了复选框,如果没有,则将一个类添加到terms_div.请帮我解决这个问题.谢谢
给定一个包含xlsx文件的流对象,我想将其保存为临时文件,并在不再使用该文件时将其删除.
我想创建一个class实现IDisposable并与using代码块一起使用它以便在最后删除临时文件.
知道如何将流保存到临时文件并在使用结束时将其删除?
谢谢
我正在尝试测试我班级的受保护方法和构造函数.为此,我尝试将其子类化,并使用C++ 11 using关键字将其成员重新导出为public :
class Foo {
protected:
Foo(int i) {}
void run() {}
};
class TestableFoo : public Foo {
public:
using Foo::Foo;
using Foo::run;
};
int main() {
TestableFoo foo(7);
foo.run();
}
Run Code Online (Sandbox Code Playgroud)
但是,g ++和clang ++都无法编译它,产生以下错误:
test.cpp:13:15: error: ‘TestableFoo::TestableFoo(int)’ is protected
using Foo::Foo;
^
test.cpp:18:16: error: within this context
TestableFoo foo(7);
^
Run Code Online (Sandbox Code Playgroud)
即使run方法公开(我单独确认),TestableFoo构造函数仍然受到保护.为什么会这样?我可以理解决策(继承与覆盖可见性),但为什么方法和构造函数之间存在不一致?
假设我们有一个基类Rectangle和一个派生类Square:
namespace Shapes {
using System.Foo;
public class Rectangle {
public Rectangle(int l, int w){}
}
}
namespace Shapes {
public class Square : Rectangle
public Square(int l, int w){}
}
Run Code Online (Sandbox Code Playgroud)
该Square课程是否必须明确说它正在使用System.Foo?我的结果变得不稳定了.在一个项目中,using指令似乎是继承的,而在Web应用程序中则不是.
考虑一个b具有两个重载方法的类foo:
struct b {
void foo(float) {}
void foo(const char *) {}
};
Run Code Online (Sandbox Code Playgroud)
如果我得到d private来自LY b,我可以用using揭露b的foo:
struct d : private b {
using b::foo;
};
Run Code Online (Sandbox Code Playgroud)
但是,这会暴露所有重载.有没有办法只暴露其中一个(比方说float一个)?例如,在下面,我想最后一行编译失败:
d t;
t.foo(3.13); // d should have this overload
t.foo("hello"); // d shouldn't have this overload
Run Code Online (Sandbox Code Playgroud)
我尝试了各种写作方式
using b::<i mean only void foo(float), dammit!>
Run Code Online (Sandbox Code Playgroud)
但无法将其中任何一个编译.
而且,显然可以d在所需的过载调用b过载中定义
struct d : private b {
void foo(float …Run Code Online (Sandbox Code Playgroud)
public class Abbreviation
{
public string ShortName { get; set; }
public string LongName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个缩写对象列表,如下所示:
List abbreviations = new List();
abbreviations.add(new Abbreviation() {ShortName = "exp.", LongName = "expression"});
abbreviations.add(new Abbreviation() {ShortName = "para.", LongName = "paragraph"});
abbreviations.add(new Abbreviation() {ShortName = "ans.", LongName = "answer"});
string test = "this is a test exp. in a para. contains ans. for a question";
string result = test.Replace("exp.", "expression")
...
Run Code Online (Sandbox Code Playgroud)
我希望结果是:"这是一个段落中的测试表达式,包含一个问题的答案"
目前我在做:
foreach (Abbreviation abbreviation in abbreviations)
{
test …Run Code Online (Sandbox Code Playgroud) using ×10
c# ×4
c++ ×3
inheritance ×3
c++11 ×1
checkbox ×1
dispose ×1
idisposable ×1
include ×1
javascript ×1
linq ×1
namespaces ×1
oracle ×1
replace ×1
resharper ×1
status ×1
stream ×1
string ×1
testing ×1
vb.net ×1