C# 后藤就在这里吗?
哦,天哪,C#中关于GOTO语句的激情;我甚至不敢问这个问题 那么多类似的问题,;这也让我有点紧张。但我是认真的 请抵制那些全盘否定GOTO声明的回复 然而,我有点困惑,为什么这个实现对GOTO来说并不理想:C# 后藤就在这里吗?,c#,goto,C#,Goto,哦,天哪,C#中关于GOTO语句的激情;我甚至不敢问这个问题 那么多类似的问题,;这也让我有点紧张。但我是认真的 请抵制那些全盘否定GOTO声明的回复 然而,我有点困惑,为什么这个实现对GOTO来说并不理想: public event CancelEventHandler DeleteSnapshotStarted; public event AsyncCompletedEventHandler DeleteSnapshotCompleted; public void DeleteSnapsho
public event CancelEventHandler DeleteSnapshotStarted;
public event AsyncCompletedEventHandler DeleteSnapshotCompleted;
public void DeleteSnapshot(Guid documentId, Action<Exception> callback)
{
if (!this.Snapshots.Where(x => x.DocumentId == documentId).Any())
throw new Exception("Snapshot not found; ensure LoadSnapshots()");
// define action
var _Action = new Action(() =>
{
// preview
bool _Cancelled = false;
if (DeleteSnapshotStarted != null)
{
CancelEventArgs _CancelArgs = new CancelEventArgs { };
DeleteSnapshotStarted(this, _CancelArgs);
if (_CancelArgs.Cancel)
{
_Cancelled = true;
goto END;
}
}
// execute
Exception _Error = null;
try
{
Proxy.CoreService.DeleteSnapshot(documentId);
LoadSnapshots(null);
}
catch (Exception ex) { _Error = ex; }
END:
// complete
if (DeleteSnapshotCompleted != null)
{
AsyncCompletedEventArgs _CompleteArgs =
new AsyncCompletedEventArgs(_Error, _Cancelled, null);
DeleteSnapshotCompleted(this, _CompleteArgs);
}
// bubble error
if (_Error != null)
throw _Error;
});
// run it
if (callback == null) { _Action(); }
else
{
using (BackgroundWorker _Worker = new BackgroundWorker())
{
_Worker.DoWork += (s, arg) => { _Action(); };
_Worker.RunWorkerCompleted += (s, arg) => { callback(arg.Error); };
_Worker.RunWorkerAsync();
}
}
}
公共事件CancelEventHandler DeleteSnapshot已启动;
公共事件AsyncCompletedEventHandler DeleteSnapshotCompleted;
公共void DeleteSnapshot(Guid documentId,操作回调)
{
如果(!this.Snapshots.Where(x=>x.DocumentId==DocumentId).Any())
抛出新异常(“未找到快照;确保LoadSnapshots()”;
//定义动作
var_Action=新操作(()=>
{
//预演
bool _Cancelled=false;
如果(DeleteSnapshotStarted!=null)
{
CancelEventArgs _CancelArgs=新的CancelEventArgs{};
DeleteSnapshot已启动(此,\u取消参数);
如果(_CancelArgs.Cancel)
{
_取消=真;
转到终点;
}
}
//执行
异常_Error=null;
尝试
{
Proxy.CoreService.DeleteSnapshot(documentId);
加载快照(空);
}
catch(异常ex){u Error=ex;}
完:
//完整的
如果(DeleteSnapshotCompleted!=null)
{
AsyncCompletedEventArgs\u CompleteArgs=
新的AsyncCompletedEventArgs(\u错误,\u取消,空);
删除快照已完成(此,_CompleteArgs);
}
//气泡误差
如果(_Error!=null)
抛出错误;
});
//运行它
如果(callback==null){u Action();}
其他的
{
使用(BackgroundWorker\u Worker=new BackgroundWorker())
{
_Worker.DoWork+=(s,arg)=>{u Action();};
_Worker.RunWorkerCompleted+=(s,arg)=>{callback(arg.Error);};
_Worker.RunWorkerAsync();
}
}
}
**我给-我会避免去D**
以下是看起来最好的:
public event CancelEventHandler DeleteSnapshotStarted;
public event AsyncCompletedEventHandler DeleteSnapshotCompleted;
public void DeleteSnapshot(Guid documentId, Action<Exception> callback)
{
if (!this.Snapshots.Where(x => x.DocumentId == documentId).Any())
throw new Exception("Snapshot not found; ensure LoadSnapshots()");
// define action
var _Action = new Action(() =>
{
// preview
CancelEventArgs _CancelArgs = new CancelEventArgs { };
if (DeleteSnapshotStarted != null)
DeleteSnapshotStarted(this, _CancelArgs);
// execute
Exception _Error = null;
if (!_CancelArgs.Cancel) try
{
Proxy.CoreService.DeleteSnapshot(documentId);
LoadSnapshots(null);
}
catch (Exception ex) { _Error = ex; }
// complete
if (DeleteSnapshotCompleted != null)
DeleteSnapshotCompleted(this,
new AsyncCompletedEventArgs(null, _CancelArgs.Cancel, null));
// bubble
if (_Error != null)
throw _Error;
});
// run it
if (callback != null)
{
using (BackgroundWorker _Worker = new BackgroundWorker())
{
_Worker.DoWork += (s, arg) => { _Action(); };
_Worker.RunWorkerCompleted += (s, arg) =>
{ callback(arg.Error); };
_Worker.RunWorkerAsync();
}
}
else
_Action();
}
公共事件CancelEventHandler DeleteSnapshot已启动;
公共事件AsyncCompletedEventHandler DeleteSnapshotCompleted;
公共void DeleteSnapshot(Guid documentId,操作回调)
{
如果(!this.Snapshots.Where(x=>x.DocumentId==DocumentId).Any())
抛出新异常(“未找到快照;确保LoadSnapshots()”;
//定义动作
var_Action=新操作(()=>
{
//预演
CancelEventArgs _CancelArgs=新的CancelEventArgs{};
如果(DeleteSnapshotStarted!=null)
DeleteSnapshot已启动(此,\u取消参数);
//执行
异常_Error=null;
如果(!\u CancelArgs.Cancel)尝试
{
Proxy.CoreService.DeleteSnapshot(documentId);
加载快照(空);
}
catch(异常ex){u Error=ex;}
//完整的
如果(DeleteSnapshotCompleted!=null)
删除快照已完成(此,
新的AsyncCompletedEventArgs(null,_CancelArgs.Cancel,null));
//泡泡
如果(_Error!=null)
抛出错误;
});
//运行它
if(回调!=null)
{
使用(BackgroundWorker\u Worker=new BackgroundWorker())
{
_Worker.DoWork+=(s,arg)=>{u Action();};
_Worker.RunWorkerCompleted+=(s,arg)=>
{回调(arg.Error);};
_Worker.RunWorkerAsync();
}
}
其他的
_动作();
}
谢谢大家。从外观上看,如果(!\u取消){…},您可以使用
包装try/catch
。目前,按照您的方式(从您提供的代码中),您没有在任何地方使用\u Cancelled
。新代码如下所示:
public event CancelEventHandler DeleteSnapshotStarted;
public event AsyncCompletedEventHandler DeleteSnapshotCompleted;
public void DeleteSnapshot(Guid documentId, Action<Exception> callback)
{
if (!this.Snapshots.Where(x => x.DocumentId == documentId).Any())
throw new Exception("Snapshot not found; ensure LoadSnapshots()");
// define action
var _Action = new Action(() =>
{
// preview
bool _Cancelled = false;
if (DeleteSnapshotStarted != null)
{
CancelEventArgs _CancelArgs = new CancelEventArgs { };
DeleteSnapshotStarted(this, _CancelArgs);
if (_CancelArgs.Cancel)
{
_Cancelled = true;
}
}
if (!_Cancelled) {
// execute
Exception _Error = null;
try
{
Proxy.CoreService.DeleteSnapshot(documentId);
LoadSnapshots(null);
}
catch (Exception ex) { _Error = ex; }
}
// complete
if (DeleteSnapshotCompleted != null)
{
AsyncCompletedEventArgs _CompleteArgs =
new AsyncCompletedEventArgs(_Error, _Cancelled, null);
DeleteSnapshotCompleted(this, _CompleteArgs);
}
// bubble error
if (_Error != null)
throw _Error;
});
// run it
if (callback == null) { _Action(); }
else
{
using (BackgroundWorker _Worker = new BackgroundWorker())
{
_Worker.DoWork += (s, arg) => { _Action(); };
_Worker.RunWorkerCompleted += (s, arg) => { callback(arg.Error); };
_Worker.RunWorkerAsync();
}
}
}
公共事件CancelEventHandler DeleteSnapshot已启动;
公共事件AsyncCompletedEventHandler DeleteSnapshotCompleted;
公共void DeleteSnapshot(Guid documentId,操作回调)
{
如果(!this.Snapshots.Where(x=>x.DocumentId==DocumentId).Any())
抛出新异常(“未找到快照;确保LoadSnapshots()”;
//定义动作
var_Action=新操作(()=>
{
//预演
bool _Cancelled=false;
如果(DeleteSnapshotStarted!=null)
{
CancelEventArgs _CancelArgs=新的CancelEventArgs{};
DeleteSnapshot已启动(此,\u取消参数);
如果(_CancelArgs.Cancel)
{
_取消=真;
}
}
如果(!\u取消){
//执行
异常_Error=null;
尝试
{
Proxy.CoreService.DeleteSnapshot(documentId);
加载快照(空);
}
catch(异常ex){u Error=ex;}
}
//完整的
如果(DeleteSnapshotCompleted!=null)
{
AsyncCompletedEventArgs\u CompleteArgs=
新的AsyncCompletedEventArgs(\u错误,\u取消,空);
删除快照已完成(此,_CompleteArgs);
}
//气泡误差
如果(_Error!=null)
抛出错误;
});
//运行它
如果(callback==null){u Action();}
其他的
{
使用(BackgroundWorker\u Worker=new BackgroundWorker())
{
_Worker.DoWork+=(s,arg)=>{u Action();};
_Worker.RunWorkerCompleted+=(s,arg)=>{callback(arg.Error);};
_Worker.RunWorkerAsync();
}
}
}
一般来说:
相反,重构代码以使goto不再是必需的,这将更加清晰和易于维护。这是一个很大的方法,应该分解一下
有时候goto是一个不错的选择,但很多时候,它倾向于在一个简单的重构就足够的时候使用
x.DocumentId == documentId).Any())
throw new Exception("Snapshot not found; ensure LoadSnapshots()");
// define action
var _Action = new Action(() =>
{
// preview
bool _Cancelled = false;
if (DeleteSnapshotStarted != null)
{
CancelEventArgs _CancelArgs = new CancelEventArgs { };
DeleteSnapshotStarted(this, _CancelArgs);
if (_CancelArgs.Cancel)
{
_Cancelled = true;
goto END;
}
}
if(!_Cancelled) {
// execute
Exception _Error = null;
try
{
Proxy.CoreService.DeleteSnapshot(documentId);
LoadSnapshots(null);
}
catch (Exception ex) { _Error = ex; }
}
END:
// complete
if (DeleteSnapshotCompleted != null)
{
AsyncCompletedEventArgs _CompleteArgs =
new AsyncCompletedEventArgs(_Error, _Cancelled, null);
DeleteSnapshotCompleted(this, _CompleteArgs);
}
// bubble error
if (_Error != null)
throw _Error;
});
if (_CancelArgs.Cancel)
{
_Cancelled = true;
goto END;
}
_Cancelled = _CancelArgs.Cancel;
if(!Cancelled)
{
// complete...
public event CancelEventHandler DeleteSnapshotStarted;
public event AsyncCompletedEventHandler DeleteSnapshotCompleted;
public void DeleteSnapshot(Guid documentId, Action<Exception> callback)
{
if (!this.Snapshots.Where(x => x.DocumentId == documentId).Any())
throw new Exception("Snapshot not found; ensure LoadSnapshots()");
// define action
var _Action = new Action(() =>
{
// preview
bool _Cancelled = false;
if (DeleteSnapshotStarted != null)
{
CancelEventArgs _CancelArgs = new CancelEventArgs { };
DeleteSnapshotStarted(this, _CancelArgs);
if (_CancelArgs.Cancel)
{
_Cancelled = true;
}
}
if (!_Cancelled) {
// execute
Exception _Error = null;
try
{
Proxy.CoreService.DeleteSnapshot(documentId);
LoadSnapshots(null);
}
catch (Exception ex) { _Error = ex; }
}
// complete
if (DeleteSnapshotCompleted != null)
{
AsyncCompletedEventArgs _CompleteArgs =
new AsyncCompletedEventArgs(_Error, _Cancelled, null);
DeleteSnapshotCompleted(this, _CompleteArgs);
}
// bubble error
if (_Error != null)
throw _Error;
});
// run it
if (callback == null) { _Action(); }
else
{
using (BackgroundWorker _Worker = new BackgroundWorker())
{
_Worker.DoWork += (s, arg) => { _Action(); };
_Worker.RunWorkerCompleted += (s, arg) => { callback(arg.Error); };
_Worker.RunWorkerAsync();
}
}
}
public event CancelEventHandler DeleteSnapshotStarted;
public event AsyncCompletedEventHandler DeleteSnapshotCompleted;
public void DeleteSnapshot(Guid documentId, Action<Exception> callback)
{
if (!this.Snapshots.Where(x => x.DocumentId == documentId).Any())
throw new Exception("Snapshot not found; ensure LoadSnapshots()");
// define action
var _Action = new Action(() =>
{
// preview
bool _Cancelled = false;
if (DeleteSnapshotStarted != null)
{
CancelEventArgs _CancelArgs = new CancelEventArgs { };
DeleteSnapshotStarted(this, _CancelArgs);
if (_CancelArgs.Cancel)
{
_Cancelled = true;
}
}
if(!_Cancelled)
{
// execute
Exception _Error = null;
try
{
Proxy.CoreService.DeleteSnapshot(documentId);
LoadSnapshots(null);
}
catch (Exception ex) { _Error = ex; }
}
// END:
// complete
if (DeleteSnapshotCompleted != null)
{
AsyncCompletedEventArgs _CompleteArgs =
new AsyncCompletedEventArgs(_Error, _Cancelled, null);
DeleteSnapshotCompleted(this, _CompleteArgs);
}
// bubble error
if (_Error != null)
throw _Error;
});
// run it
if (callback == null) { _Action(); }
else
{
using (BackgroundWorker _Worker = new BackgroundWorker())
{
_Worker.DoWork += (s, arg) => { _Action(); };
_Worker.RunWorkerCompleted += (s, arg) => { callback(arg.Error); };
_Worker.RunWorkerAsync();
}
}
}
// Column-ordered, first value search:
int valueFound = 0;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (array[j, i] < targetValue)
{
valueFound = array[j, i];
goto Found;
}
}
}
Console.WriteLine("No value was found.");
return;
Found:
Console.WriteLine("The number found was {0}.", valueFound);
if(!_Cancelled)
{
Exception _Error = null;
try
{
Proxy.CoreService.DeleteSnapshot(documentId);
LoadSnapshots(null);
}
catch (Exception ex) { _Error = ex; }
}
switch (CancleArgs)
{
case Canceled: _Cancelled = true; break;
... other stuff
case END:
{
// complete
if (DeleteSnapshotCompleted != null)
{
AsyncCompletedEventArgs _CompleteArgs = new AsyncCompletedEventArgs(_Error, _Cancelled, null);
DeleteSnapshotCompleted(this, _CompleteArgs);
}
// bubble error
if (_Error != null) throw _Error;
}
break;
}
// run it
...