C# Goto妙用

发布时间 2023-06-19 16:58:46作者: 喜爱糖葫芦

提问

你用过goto吗,如何使用呢,应该使用吗?

回答

妙用:当发生错误时回到某个位置
很显然这破坏了程序的顺序性,但确实很有意思,只要不到处用

示例

Found:
 {
      Console.WriteLine("请输入swagger.json的url:");
      requestUri = Console.ReadLine();
  }
  Stream stream;
  var client = new HttpClient();
  try
  {
      stream = await client.GetStreamAsync(requestUri);
  }
  catch (Exception e)
  {
      goto Found;
  }