以下'using'语句的版本I和II都有效,但我怀疑第一个版本是否有效,因为Visual Studio 2010中的C#垃圾收集器尚未删除变量"context"(实体框架变量).另一方面,我从一个看似有信誉的来源网上获得了第一个版本,所以我认为它没关系?
版本I:
try
{
using ( AnEFEntity context = new AnEFEntity()) //note: no curly brackets!
using (var ts = new System.Transactions.TransactionScope())
{
// stuff here that uses variable context
}
}
catch (Exception ex)
{
}
Run Code Online (Sandbox Code Playgroud)
//以上编译很好并且工作正常 - 但是在范围内是第一个'使用'语句?似乎是这样,但它是可疑的.
版本II:
try
{
using ( AnEFEntity context = new AnEFEntity())
{ //note a curly bracket used for first ‘using’
using (var ts = new System.Transactions.TransactionScope())
{
// stuff here that uses variable context
}
} //note the placement of …Run Code Online (Sandbox Code Playgroud) 在同步模型中,它很简单
using (MyServiceClient msc = new MyServiceClient())
{
msc.Method();
}
Run Code Online (Sandbox Code Playgroud)
但如果我必须等到这个方法的结束,然后做一些事情,它就无法工作
private void EventHandler<MethodCompletedEventArgs> myEventHandler = new EventHandler<MethodCompletedEventArgs>(methodBody);
using (MyServiceClient msc = new MyServiceClient())
{
msc.MethdCompleted += myEventHandler;
msc.BeginMethod();
}
private void MethodBody()
{
//exception: client state is aborted
}
Run Code Online (Sandbox Code Playgroud)
另外如何在using语句中调用异步mehod ?
我的项目中有几个组合框.他们的内容正在发生很大变化.所以我将他们的内容放在我的数据库中.现在,我编写了一个类似于下面的代码来检索他们的数据,但我想知道,这种"使用"是否像我的方法一样工作或根本不重要?我的主要问题是; 这是一种健康的方式来使用这样的对象吗?
DataTable results;
using (results = myDataInteractionClass.SelectDB())
{
// fill table objects to combobox
}
using (results = myDataInteractionClass.SelectAnotherDB())
{
// fill this new table's objects to combobox
}
using (results = myDataInteractionClass.SelectThirdDB())
{
// so goes on like this.
}
Run Code Online (Sandbox Code Playgroud) 我对以下代码以及using语句及其对象处理的范围感到困惑.
using(DbFactory db = new DbFactory())
{
Repository repo = new Repository<someobject>(db);
result = repo.Get(somecondition);
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,使用块执行后会发生什么DbFactory?using语句中使用
的repo变量范围是什么?
它DbFactory被使用,Repository并且它有一个成员变量,它将保存DbFactory.那么这将处置DbFactory吗?
EDIT1:
Repository repo;
ResultObject result;
using(DbFactory db = new DbFactory())
{
repo = new Repository<someobject>(db);
result = repo.Get(somecondition);
}
public class Repository
{
private _dbFactory;
public Repository(DbFactory dbFactory)
{
_dbFactory = dbFactory;
}
}
Run Code Online (Sandbox Code Playgroud)
现在DbFactory会在using语句之后被处理掉吗?
我正在使用Visual Studio 2017编写C#应用程序.我努力通过使用"using"语句来处理我实例化的所有对象.如果我实例化一个不基于可隐式转换为'System.IDisposable'的类型的对象,Visual Studio会发出警告.此示例导致VS显示警告(C#):
using (uri = new System.Uri(stringVarWithPath))
{
}
Run Code Online (Sandbox Code Playgroud)
是否所有未实现Dispose方法的类型都不受管理?我问,因为"实现Dispose方法"(https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose)似乎暗示它仅适用于非托管资源.
c# garbage-collection dispose using-statement unmanagedresources
我看到了大量带有以下语法的代码片段
using (RandomType variable = new RandomType(1,2,3))
{
// A bunch of code here.
}
Run Code Online (Sandbox Code Playgroud)
为什么不只是声明变量并使用它?
这种使用语法似乎只是使代码混乱并使其可读性降低.如果重要的是那个变量只在那个范围内可用,为什么不把这个块放在一个函数中呢?
当一个字段用于创建另一个字段,然后处理 - 我看到两个选项.第一:
Image image = Image.FromFile(path);
Bitmap bitmap = (Bitmap)image.GetThumbnailImage(32, 32, null, new IntPtr());
image.Dispose();//not needed anymore.
//use bitmap
bitmap.Dispose();
Run Code Online (Sandbox Code Playgroud)
第二:
using (Image image = Image.FromFile(path))
{
using (Bitmap bitmap = (Bitmap)image.GetThumbnailImage(32, 32, null, new IntPtr()))
{
//use bitmap
}
}
Run Code Online (Sandbox Code Playgroud)
逻辑1将是第一个(因为在使用Image时不需要Bitmap),但using语句通常优先于Dispose.
还有第三种方法 - using在第二种方式中终止第一种方式吗?
我需要在两个单独的文件中写两个日志:一个每秒写入530个字符30次,另一个每秒写入60个60次.我将保存将写入两个独立变量的数据,并分别写入n和m帧.要编写我使用的变量:
using (StreamWriter writer = new StreamWriter(newFileName, true))
Run Code Online (Sandbox Code Playgroud)
然后
writer.Write(data)
Run Code Online (Sandbox Code Playgroud)
现在......我知道使用"使用"的优点,但我想知道:它有开销吗?为什么不在代码的开头声明StreamWriter并在需要时使用它?
根据本网页上的内容,如果我理解正确,该using语句就像a一样try/finally,所以我可能错误地认为如果在using语句中发生异常,它不应该使程序崩溃.
但是,当DownloadString下面的示例中显示的方法抛出a时WebException,程序崩溃.
using (WebClient client = new WebClient())
{
string response = client.DownloadString(url);
// ...
}
Run Code Online (Sandbox Code Playgroud)
这是正常的,因为using语句不像a那样工作try/catch/finally,实际上没有处理异常.但后来我想知道这个using声明的目的是什么.
更新...根据以下回复,我添加了以下注意事项.基本上,如果我需要处理异常,可能的解决方案可能如下.
using语句放在一个try/catch块中.DonwloadString方法放在try/catch块中.第三种解决方案的示例代码.
WebClient client = new WebClient();
try
{
string response = client.DownloadString(url);
// ...
}
catch(Exception ex)
{
// handle (or ignore) the exception
}
finally
{
if (client != null)
client.Dispose(); …Run Code Online (Sandbox Code Playgroud) c# ×10
using-statement ×10
.net ×2
asynchronous ×1
dispose ×1
exception ×1
streamwriter ×1
try-finally ×1
wcf ×1