相关疑难解决方法(0)

"开启类型"还有比这更好的选择吗?

看作C#无法打开一个Type(我收集的并不是作为特殊情况添加的,因为is-a关系意味着可能有多个不同的情况可能适用),是否有更好的方法来模拟切换类型?

void Foo(object o)
{
    if (o is A)
    {
        ((A)o).Hop();
    }
    else if (o is B)
    {
        ((B)o).Skip();
    }
    else
    {
        throw new ArgumentException("Unexpected type: " + o.GetType());
    }
}
Run Code Online (Sandbox Code Playgroud)

c# switch-statement system.type

312
推荐指数
15
解决办法
10万
查看次数

C#switch语句限制 - 为什么?

在编写switch语句时,在case语句中可以打开的内容似乎存在两个限制.

例如(是的,我知道,如果你正在做这种事情,这可能意味着你的面向对象(OO)架构是不确定的 - 这只是一个人为的例子!),

  Type t = typeof(int);

  switch (t) {

    case typeof(int):
      Console.WriteLine("int!");
      break;

    case typeof(string):
      Console.WriteLine("string!");
      break;

    default:
      Console.WriteLine("unknown!");
      break;
  }
Run Code Online (Sandbox Code Playgroud)

这里switch()语句失败,带有'一个预期的整数类型的值',case语句失败并带有'a expected value is expected'.

为什么会有这些限制,以及基本理由是什么?我看不出有任何理由switch语句具有只能屈从于静态分析,为什么在接通的值必须是完整的(即原语).理由是什么?

c# switch-statement

136
推荐指数
8
解决办法
10万
查看次数

C#中的常量数组

我开始学习C#几天,如果这是一个愚蠢的问题!我有一个像这样的字符串数组

private readonly string[] algorithm_list = {
                                              "Genetic Algorithm",
                                              "Dynamic Algorithm"
                                          };
Run Code Online (Sandbox Code Playgroud)

和我的代码

switch (al_choose)
            {
                case algorithm_list[0]:
                    break;
                case algorithm_list[1]:
                    break;
                default:

            }
Run Code Online (Sandbox Code Playgroud)

错误是algorithm_list [0]不是常量!所以我尝试其他声明

private readonly string[] algorithm_list 
Run Code Online (Sandbox Code Playgroud)

要么

private contant string[] algorithm_list
Run Code Online (Sandbox Code Playgroud)

但它仍然无法正常工作???? 那么,对我有什么建议吗?非常感谢!

c# arrays

1
推荐指数
1
解决办法
654
查看次数

使用 ToLower() 进行切换会导致错误 CS0426 类型名称 '' 在类型 '' 中不存在

为什么编译器仅在 switch 语句中为常量值调用方法时才会抱怨,为什么会出现错误The type name 'A' does not exist in the type

CS0426 类型名称“A”在类型“ClassificationIdentifiers.ClassificationIdentifiersChildren”中不存在

public static class ClassificationIdentifiers
{
    public static class ClassificationIdentifiersChildren
    {
        public const string A = "A";
    }
}

switch (classificationFileType)
{
    case ClassificationIdentifiers.ClassificationIdentifiersChildren.A:
        classification = ClassificationIdentifiers.ClassificationIdentifiersChildren.A;
        break;
}

switch (classificationFileType)
{
    case ClassificationIdentifiers.ClassificationIdentifiersChildren.A.ToLower():
        classification = ClassificationIdentifiers.ClassificationIdentifiersChildren.A;
        break;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我认为这与下面的错误有关,"A".ToLower();或者case a.ToLower():

const string a = "A".ToLower();

switch (classificationFileType)
{
    case a.ToLower():
        classification = ClassificationIdentifiers.ClassificationIdentifiersChildren.A;
        break;
}
Run Code Online (Sandbox Code Playgroud)

CS0133 分配给“a”的表达式必须是常量

CS0118 'a' 是一个变量,但像类型一样使用

在此输入图像描述

c#

1
推荐指数
1
解决办法
774
查看次数

是否可以在switch语句中调用方法?

简单的问题.是否可以switch在C#中的语句中调用方法?要求.NET> = 4.5

var x = "Hello World";
switch(x)
{
    case "Foo":
        break;
    // What I actually want to do
    case x.StartsWith("Hello"):
        return "Bar";
}
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
97
查看次数

预期值为恒定值

我这里遇到问题了。

void Sre_Reconhecimento(object sender, SpeechRecognizedEventArgs e)
    {
        string text = System.IO.File.ReadAllText(@"C:\Users\ADMIN25\Desktop\testing.txt");
        string[] words = text.Split(',');
        switch (e.Result.Text)
        {

            case words[0]:
                MessageBox.Show("works!");
                break;
            case words[1]:
                MessageBox.Show("works too!");
                break;
        }

    }
Run Code Online (Sandbox Code Playgroud)

当我尝试运行该程序时,出现此错误:需要一个常量值。

如何在不使用if/elseif case 的情况下修复它?

c#

-1
推荐指数
1
解决办法
9429
查看次数

标签 统计

c# ×6

switch-statement ×2

arrays ×1

system.type ×1