我有C#几个项目的解决方案Visual Studio 2010.一个是测试项目(我将其称为" PrjTest "),另一个是Windows Forms Application项目(我将其称为" PrjForm ").还有一个由PrjForm引用的第三个项目,它能够成功引用和使用.
PrjForm引用PrjTest,而PrjForm有一个带有using语句的类:
using PrjTest;
Run Code Online (Sandbox Code Playgroud)
using 声明正确到位using PrjTest;出现错误:找不到类型或命名空间名称'PrjTest'(您是否缺少using指令或程序集引用?)
我尝试过以下方法来解决这个问题:
VS 2010我做完了我的功课,花了很长时间在线寻找答案,但这些解决方案都没有帮助.
我还能尝试什么?
c# reference using-directives using-statement visual-studio-2010
通过以某种方式在同一个使用块中声明2变量,是否可以使这个代码更紧凑?
using (var sr = new StringReader(content))
{
using (var xtr = new XmlTextReader(sr))
{
obj = XmlSerializer.Deserialize(xtr) as TModel;
}
}
Run Code Online (Sandbox Code Playgroud) 在C#中使用块的目的是什么?它与局部变量有什么不同?
就像是:
using (IDisposable disposable = GetSomeDisposable())
{
//.....
//......
return Stg();
}
Run Code Online (Sandbox Code Playgroud)
我相信这不是回报声明的合适地方,是吗?
哪一个:
using (var myObject = new MyClass())
{
try
{
// something here...
}
catch(Exception ex)
{
// Handle exception
}
}
Run Code Online (Sandbox Code Playgroud)
要么
try
{
using (var myObject = new MyClass())
{
// something here...
}
}
catch(Exception ex)
{
// Handle exception
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码
using(MemoryStream ms = new MemoryStream())
{
//code
return 0;
}
Run Code Online (Sandbox Code Playgroud)
dispose()在using语句括号结束时调用该方法}对吗?由于我 return在using声明结束之前,MemoryStream对象是否会被妥善处理?这里发生了什么?
C#允许我执行以下操作(来自MSDN的示例):
using (Font font3 = new Font("Arial", 10.0f),
font4 = new Font("Arial", 10.0f))
{
// Use font3 and font4.
}
Run Code Online (Sandbox Code Playgroud)
如果发生什么font4 = new Font抛出?从我的理解,font3将泄漏资源,不会被处置.
using(... , ...)应该完全避免使用嵌套使用?Java是否有一个在hibernate中打开会话时可以使用的using语句?
在C#中它是这样的:
using (var session = new Session())
{
}
Run Code Online (Sandbox Code Playgroud)
因此,对象超出范围并自动关闭.
我有一种情况,我正在async调用返回和IDisposable实例的方法.例如:
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com"));
Run Code Online (Sandbox Code Playgroud)
现在async,在使用IDisposable实例之前,使用"response"变量的调用和代码将包含在using语句中.
我的问题是,当async混合中抛出关键字时,这仍然是正确的方法吗?即使代码编译,using语句是否仍然可以在下面的示例中按预期工作?
例1
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
// Do something with the response
return true;
}
Run Code Online (Sandbox Code Playgroud)
例2
using(HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")))
{
await this.responseLogger.LogResponseAsync(response);
return true;
}
Run Code Online (Sandbox Code Playgroud) using-statement ×10
c# ×9
using ×3
.net ×2
dispose ×2
idisposable ×2
asynchronous ×1
hibernate ×1
java ×1
reference ×1
syntax ×1
try-catch ×1