标签: switch-statement

C#中的案例声明块级声明空间

有没有理由我错过了case语句中的块不被视为块级声明空间?

我尝试时不断收到错误(变量已经声明)

case x:
  var someVariable = 42;
break;
case y: 
   var someVariable = 40;
break;
Run Code Online (Sandbox Code Playgroud)

但我能做到

case x:
   try{var someVariable = 42;}catch{} 
break;
case y: 
    try{var someVariable = 40;}catch{}
break;
Run Code Online (Sandbox Code Playgroud)

如果C#允许通过语句,那就没有意义,但事实并非如此,我想不出你可以在case语句中声明变量并在该块之外使用它的场景.

c# language-design switch-statement

7
推荐指数
3
解决办法
1679
查看次数

如何使用数组值进行大小写切换(不是数组编号)

你如何使用数组的VALUE而不是数组中用于确定大小写的数字?在我的代码中:

for (int x = 0; x < 3; x++)
        {
            switch (position[x])
            {
                case 0:
                    label1.Text = people[x];
                    break;
                case 1:
                    label2.Text = people[x];
                    break;
                case 2:
                    label3.Text = people[x];
                    break;
            }
        }
Run Code Online (Sandbox Code Playgroud)

当它运行时,它使用位置[]中的x而不是位置[x]的值来确定使用哪种情况.例如,当x为0,但position [x]的值为1时,它使用大小写0.如何获取该值?

编辑:我的代码确实是问题....由于某种原因早上调试有创建虚假图像的效果...:P作为一个FYI,这是正确的代码...

for (int x = 0; x < 3; x++)
        {
            if (position[x] == 2)
            {
                position[x] = 0;
            }

            else
            position[x]++;

        }

        for (int x = 0; x < 3; x++)
        {
            int val = position[x];
            switch (val)
            {
                case 0:
                    label1.Text …
Run Code Online (Sandbox Code Playgroud)

.net c# arrays switch-statement

7
推荐指数
1
解决办法
2万
查看次数

SVN Switch不会删除切换到标签或分支时不存在的工作中的文件夹/文件

我需要根据标记或分支进行构建.所以我已经自动化我的构建过程,以便在他们要求构建时切换到标记或分支.我使用svn switch命令使工作副本指向标签或从主干工作副本分支.问题是:如果头版本中存在新添加的文件夹,则不会从工作副本中删除它们.它们在工作副本中仍然是无版本的,但构建中的无版本文件夹会导致很多问题.如何避免这个问题?使用svn switch命令时是否有任何选项可以避免未版本控制的文件.

svn switch-statement

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

是否有警告(错误),类似于C#C4061 for C#

通常,如果我在C#中使用switch for enums,我必须编写类似的东西:

switch (e)
{
    case E.Value1:
        //...
        break;

    case E.Value2:
        //...
        break;

        //...
    default:
        throw new NotImplementedException("...");
}
Run Code Online (Sandbox Code Playgroud)

在C++(对于VS)中,我可以为此开关启用警告C4061和C4062,使它们出错并进行编译时检查.在C#中,我必须将此检查移至运行时...

有谁知道在C#中如何在编译时检查这个?也许有一个警告,默认情况下禁用,我错过了,或者其他方式?

c# enums switch-statement compile-time

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

切换语句未初始化的变量

以下代码给了我: The local variable str may not have been initialized

public class experiment{

    public static void main(String[] args){

        int day = 1;
        String str;

        switch (day) {
        case 1 : str = "nice";
        break;
        }

        System.out.println(str);

    }


}
Run Code Online (Sandbox Code Playgroud)

所以,我给了str一个null值,它有效,但我仍然想知道为什么文档上的那个没有初始化值,我已经三重检查,我不认为我有任何错别字:

public class SwitchDemo {
    public static void main(String[] args) {

        int month = 8;
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break; …
Run Code Online (Sandbox Code Playgroud)

java switch-statement

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

意外的PHP切换行为

我正在运行一些单元测试,并使用我正在使用的switch语句遇到了意外行为.我已经隔离了下面的条件.

function test($val)
{
    switch($val)
    {
       case 'a':
       case 'b':
          return 'first';
       break;
       case 'c':
          return 'second';
       break;
       default:
          return 'third';
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的第一轮测试:

test('a') => 'first'
test('b') => 'first'
test('c') => 'second'
test('d') => 'third'    
test('0') => 'third'
test('1') => 'third'
test('true')  => 'third'
test('false') => 'third'
Run Code Online (Sandbox Code Playgroud)

这很明显吧?好了,现在看看这些:

test(0)     => 'first'  // expected 'third'
test(1)     => 'third'
test(true)  => 'first'  // expected 'third'
test(false) => 'third'
test(null)  => 'third'
test([])    => 'third'
Run Code Online (Sandbox Code Playgroud)

什么是奇怪的结果与0和真?如果1/true和0/false返回相同的值,我会把它写成松散的输入.但他们没有!

如果我将值转换为(字符串),则交换机按预期工作.

test((string) 0)     => 'third'
test((string) …
Run Code Online (Sandbox Code Playgroud)

php switch-statement

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

C#切换类型

编辑:现在可以在C#7.0中使用.


我有以下的片,检查给定的代码PropertyInfo的type.

PropertyInfo prop;

// init prop, etc...

if (typeof(String).IsAssignableFrom(prop.PropertyType)) {
    // ...
}
else if (typeof(Int32).IsAssignableFrom(prop.PropertyType)) {
    // ...
}
else if (typeof(DateTime).IsAssignableFrom(prop.PropertyType)) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

有没有办法switch在这种情况下使用语句?这是我目前的解决方案:

switch (prop.PropertyType.ToString()) {
    case "System.String":
        // ...
        break;
    case "System.Int32":
        // ...
        break;
    case "System.DateTime":
        // ...
        break;
    default:
        // ...
        break;
}
Run Code Online (Sandbox Code Playgroud)

我不认为这是最好的解决方案,因为现在我必须给出给定的完全限定String值type.有小费吗?

c# reflection switch-statement

7
推荐指数
2
解决办法
4196
查看次数

去教程选择语句

我正在浏览tour.golang.org上的示例,我遇到过这个我不太懂的代码:

package main
import "fmt"

func fibonacci(c, quit chan int) {
    x, y := 0, 1
    for {
        select {
        case c <- x: // case: send x to channel c?
            x, y = y, x+y
        case <-quit: // case: receive from channel quit?
            fmt.Println("quit")
            return
        }
    }
}

func main() {
    c := make(chan int)
    quit := make(chan int)
    go func() { // when does this get called?
        for i := 0; i < 10; i++ {
            fmt.Println(<-c)
        } …
Run Code Online (Sandbox Code Playgroud)

channel go switch-statement

7
推荐指数
2
解决办法
1939
查看次数

基于运行时参数执行整数模板化函数

我经常有一些基于某种设计方法生成输出的原型行为.我模板化设计方法,它提供了我需要的许多功能.但是,有时设计方法是在运行时给出的,所以我通常需要编写一个巨大的switch语句.它通常看起来像这样:

enum class Operation
{
    A, B
};


template<Operation O>
    void execute();

template<>
    void execute<A>()
    {
        // ...
    }

template<>
    void execute<B>()
    {
        // ...
    }

void execute(Operation o)
{
    switch (o)
    {
    case Operation::A: return execute<Operation::A>();
    case Operation::B: return execute<Operation::B>();
    }
}
Run Code Online (Sandbox Code Playgroud)

我很好奇是否有人为这个系统找到了一个很好的模式 - 这个方法的主要缺点是必须输出所有支持的枚举,并且如果实现了新的枚举,则需要维护几个地方.

e:我应该补充说,弄乱编译时模板的原因是允许编译器在HPC中内联方法以及继承constexpr属性.

e2:实际上,我想我要问的是让编译器使用隐式开关结构生成所有可能的代码路径.也许一些递归模板魔术?

c++ metaprogramming switch-statement

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

C#7使用空检查切换案例

C#7引入了一个名为patterns的新功能,您可以将其与Is-Expression或Switch案例一起使用,如下所示:

string str = null; 
switch(str){
    case string x:
        Console.WriteLine("string " + x);
        break;
    default:
        Console.WriteLine("default");
        break;
}
Run Code Online (Sandbox Code Playgroud)

并且你会期望它会进入#1的情况,因为它是相同的类型,但它没有.

c# pattern-matching switch-statement null-string c#-7.0

7
推荐指数
2
解决办法
1505
查看次数