【发布时间】:2021-11-06 02:20:44
【问题描述】:
编辑:我会重新提出问题:
是否有可能以这种方式使用 switch 语句。似乎一方面允许将类型作为案例来简化代码的外观,但是如果您想通过使用 go to 语句来实现重用,那么这是不可能的。就目前而言,我对语言结构更感兴趣,而不是实际解决我可以通过重复一些代码或转向 if else 语句来解决的问题。
我知道goto 通常很糟糕,但它似乎至少可以作为一种在 C 中模拟失败机制的方法是可以接受的,而不会因为使用/省略 break 而容易出错;误会了
我想在切换类型时使用switch 语句,但我想使用goto 来使用跌落谷
我想根据列表中元素的类型执行不同的操作:
但我收到错误:Use of unassigned local variable 'animal1' 我可以使用对象类型上的开关来执行此操作,还是需要比较字符串或使用 if-else 构造?
using System;
using System.Collections.Generic;
namespace switch_type_experiments
{
public class Animal
{
public void Eat()
{ }
}
public class Mammal : Animal
{
public void DoMammalStuff()
{ }
}
public class Dog : Mammal
{
public void Bark()
{
}
}
class Program
{
static void Main(string[] args)
{
List<Animal> listOfAnimals = new();
foreach (Animal animal in listOfAnimals)
{
switch (animal)
{
case Dog animal1:
//do stuff only applicable for dog such as accessing
// available only for dogs
animal1.Bark();
goto MammalLabel;
case Mammal animal1:
MammalLabel:
//do Mammal stuff
animal1.DoMammalStuff();
goto AnimalLabel;
case Animal animal1:
AnimalLabel:
animal1.Eat();
//do Animal stuff
break;
default:
break;
}
}
}
}
}
【问题讨论】:
-
一般来说,使用
goto是一个坏的想法。你不应该这样做,除非你确切地知道你在做什么并且你必须使用它。您能否详细说明为什么您认为 goto 是解决您的问题的最佳解决方案? (在你的情况下,一系列if可能是合适的。) -
不太确定是不是这样,但是您的错误使我相信您正在跳出animal1变量的范围。毕竟,当您来自 dog case 时,您在演员表之后跳跃(意味着您之前不执行这些东西),因此访问 animal1 会导致错误。
-
我只想从一种情况切换到另一种情况以避免代码重复,因为我的对象和逻辑比示例复杂一些。这很可能比我把事情复杂化了。现在看来,我只需要摆脱
animal1并使用animal并使用as投射它。我认为我也误解了使用类型切换的工作原理。现在不确定这个问题有多大用处,如果它得到很多反对意见,我会删除它。
标签: c# switch-statement polymorphism goto