在"使用"块内重新初始化是一个坏主意,在任何时候都要避免.我还是会问这个:
为什么"使用"调用处理原始值而不是最后一个引用或重新初始化(如果使用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>?在下面的示例中,如果代码离开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”替换。可以吗
如果HttpClient的是不应该在使用使用声明,请参阅以下链接: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ http://www.nimaara.com/2016/11 /01/beware-of-the-net-httpclient/
因此,如果您不应该在 using 语句中使用 HttpClient ,那么为什么有这么多示例将 WebClient 放在 using 语句中(包括 Microsoft);我还没有看到一篇关于不在using语句中放置 WebClient 的文章?最终在最低级别 WebClient 和 HttpClient 最终在同一个地方打开一个 TCP/IP 套接字。是否介于两者之间使 WebClient 可以放入using语句?
注意我提出这个问题是有原因的:我有一个在 WinXP、Win7 完整系统和嵌入式系统上运行的客户端应用程序,我用来将 XML POST 到我的后端主机(CentOS 系统)的代码如下所示:
void PostXml(string url, string xml, int id) {
try
{
//Console.WriteLine("POST Request: " + xml);
using (WebClient wc = new WebClient())
{
wc.Proxy = null; //DEBUG: testing this to see if it helps webclient post failure after time x
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
Byte[] d …Run Code Online (Sandbox Code Playgroud) using ×10
c# ×6
c++ ×3
alias ×2
c++11 ×2
templates ×2
.net ×1
dispose ×1
httpclient ×1
idisposable ×1
memorystream ×1
pointers ×1
process ×1
stream ×1
streamreader ×1
typedef ×1
typename ×1
webclient ×1