在"使用"块内重新初始化是一个坏主意,在任何时候都要避免.我还是会问这个:
为什么"使用"调用处理原始值而不是最后一个引用或重新初始化(如果使用try finally block则会发生这种情况)
MyClass b = new MyClass();// implements Idisposable
MyClass c = new MyClass();
MyClass a ;
using (a = new MyClass())
{
a = b;
a = c;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,dispose将在原始引用上调用,而不是在引用时更新.这可以通过在dispose方法中在控制台上打印一些内容来轻松验证.
然而,使用try {} finally代码调用最后一个引用dispose方法.
try
{
a = new MyClass();
a = b;
a = c;
}
finally
{
a.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
MSDN:using语句确保即使在对象上调用方法时发生异常,也会调用Dispose.
using (Font font1 = new Font("Arial", 10.0f))
{
byte charset = font1.GdiCharSet;
}
Run Code Online (Sandbox Code Playgroud)
基本上"使用"转换为:
{
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset …Run Code Online (Sandbox Code Playgroud) 喜欢 :
using ::size_t; using ::fpos_t; using ::FILE;
Run Code Online (Sandbox Code Playgroud)
事实上,这个问题受到了这个问题的评论的启发:
我有类似下面的代码...有人在这里提到WebClient,Stream和StreamReader对象都可以从使用块中受益.两个简单的问题:
1:这个小片段看起来如何使用块?我做自己的研究没有问题,所以资源链接很好但是看到一个例子会更快更容易,我会从中理解它.
2:我想养成良好的编码标准的习惯,如果我对使用积木更好的原因有所了解会有所帮助...是否只是让你不必担心关闭或在那里更多原因?谢谢!
WebClient client = new WebClient();
Stream stream = client.OpenRead(originGetterURL);
StreamReader reader = new StreamReader(stream);
JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
string encryptionKey = (string)jObject["key"];
string originURL = (string)jObject["origin_url"];
stream.Close()
reader.Close()
Run Code Online (Sandbox Code Playgroud) 想知道以下情况是否会导致任何内存泄漏.
Aspx页面包含以下内容.
private void Generator(input)
{
using (MemoryStream memoryStream = Helper.Instance.Generate(input))
{
}
}
Run Code Online (Sandbox Code Playgroud)
从返回内存流的aspx页面调用下面的方法.
MemoryStream Generate(input)
{
MemoryStream stream = new MemoryStream();
//doing some stream manipulation here
return stream;
}
Run Code Online (Sandbox Code Playgroud) 是否可以在不安全的指针类型上创建别名,即我想使用以下内容:using bytePtr = System.Byte*.
.net开发人员会在未来版本中引入此功能(指针和数组类型的别名)吗?
在我正在研究的项目中进行编码时,我发现了一些非常奇怪的东西:
namespace detail {
struct tuplelike_tag { };
struct arraylike_tag { };
template<typename>
struct call_with_traits;
template<typename... Ts>
struct call_with_traits<std::tuple<Ts...>> {
using tag = tuplelike_tag;
enum { size = sizeof...(Ts) };
};
template<typename T, std::size_t Sz>
struct call_with_traits<std::array<T, Sz>> {
using tag = arraylike_tag;
enum { size = Sz };
};
template<typename T, std::size_t Sz>
struct call_with_traits<T[Sz]> {
using tag = arraylike_tag;
enum { size = Sz };
};
template<typename F, typename T, int... Is>
auto call_with(F && f, T && …Run Code Online (Sandbox Code Playgroud) 我需要实现以下接口
struct mutex;
struct interface
{
//...
mutex& getMutex();
};
Run Code Online (Sandbox Code Playgroud)
我可以using mutex = ParticularMutex在我的实现中使用直觉,但gcc告诉我:
error: conflicting declaration ‘using mutex = ’
error: ‘class mutex’ has a previous declaration as ‘class mutex’
Run Code Online (Sandbox Code Playgroud)
我没有定义任何两次,只是宣布两次,就像前面宣布的那样,所以
interface?interface定义?用template <typename mutex>?我想为一个类型别名,以便在必要时可以给它一个模板参数.
template<typename T, unsigned d>
struct value
{
T a[d];
};
template<typename T=float>
using val=value<T, 2>;
int main()
{
val v; //should now be equal to val<float> v;
val<int> w; //should also be valid.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
G ++因某些原因不赞成:
test.cpp: In function ‘int main()’:
test.cpp:12:13: error: missing template arguments before ‘v’
val v; //should now be equal to val<float> v;
^
test.cpp:12:13: error: expected ‘;’ before ‘v’
Run Code Online (Sandbox Code Playgroud)
默认模板参数不能与'using'一起使用吗?如果是这样,为什么不在行上说明默认参数?
在下面的示例中,如果代码离开using语句后它仍在运行,那么该进程会发生什么?
using (var p = new Process())
{
p.StartInfo.FileName = "c:\\temp\\SomeConsoleApp.exe";
p.Start();
}
Run Code Online (Sandbox Code Playgroud) 我知道“ using”关键字可以用作模板别名和类型别名,但是我没有看到有人提到“ typedef typename”可以被“ using”替换。可以吗