是否可以从C#中的开关盒分配返回?

是否可以从C#中的开关盒分配返回?,c#,switch-statement,C#,Switch Statement,我看到的所有C#switch语句示例如下 var variable, result; switch (variable) { case 1: result = somevalue; break; case 2: result = someothervalue; break; } 然而,我想要一些像 var result = switch (variable) { case 1: return <somevalue>; case 2: return <som

我看到的所有C#switch语句示例如下

var variable, result; 

switch (variable) {
 case 1: result = somevalue; break;
 case 2: result = someothervalue; break;
}
然而,我想要一些像

var result = switch (variable) {
   case 1: return <somevalue>;
   case 2: return <someothervalue>;
  }
var结果=开关(变量){
案例1:返回;
案例2:返回;
}

可能吗?(可能我需要在案例中使用
break
,但它是一个返回…

返回
return
背后的基本思想是正确的,但
switch
是一个控制语句,因此它不返回
值。您必须使其成为这样一种方法:

dynamic SomeMethod(int variable)
{
   switch(variable)
   {
       case 1: return "text";
       case 2: return 5;

       // Or manually return something out of switch scope
       // because the method has to return something
       default: return null;
   }
}

void Test()
{
    // Now you have a value assigned to an variable
    // that comes from SomeMethod
    // which is generated (selected) by switch
    var result1 = SomeMethod(1); // string
    var result2 = SomeMethod(2); // int
    var result3 = SomeMethod(123); // null
}
在这种情况下,我还需要解释:方法不能
返回
隐式类型(
var
),因为编译器无法猜测
返回
类型是什么。但您可以
返回
,现在类型将在运行时更改。您也不能在
开关中使用
动态
,因为它需要一个类型

如果希望它简短(在方法中),可以使用lambda:)创建匿名方法

var结果=
(Func)
((x)=>
{
开关(x)
{
案例1:返回“文本”;
案例2:返回5;
默认值:返回null;
}
}//拉姆达
)//Func(接受int参数,返回动态值)
(); // 调用它并获取要分配的返回值
然而,我强烈建议您阅读一些文章,如,…

考虑:

var结果=
变量==1?:
变量==2?:
;

< /代码> 如果你想根据某个输入返回一个值,并且你寻找一个开关作为一个可能的解决方案,考虑一个字典。
// populated with types and data that mean something
// to you. 
private IDictionary<int, string> _lookupDictionary; 

public string GetValue(int variable) {
    return _lookupDictionary[variable];

    // instead of 
    // switch (variable) { 
    //     case 1: 
    //         return <somevalue>; 
    //     case 2:
    //         return <someothervalue>; 
    //     ...
    //     case n-1: 
    //         return <somethingelse>; 
    //     case n: 
    //         return <finalsomething>; 
}
//填充了有意义的类型和数据
//谢谢你。
私人IDictionary_lookupDictionary;
公共字符串GetValue(int变量){
返回_lookupDictionary[变量];
//而不是
//开关(变量){
//案例1:
//返回;
//案例2:
//返回;
//     ...
//案例n-1:
//返回;
//案例n:
//返回;
}
请注意,开关本身不返回值。如果您还想保存该返回值,则只需使用字典的索引器执行查找,如图所示,存储引用,然后返回

private IDictionary<int, string> _lookupDictionary;
private KeyValuePair<int, string> _cache; 

public string GetValue(int variable) {
    if (!_lookupDictionary.ContainsKey(variable)) {
        // throw an exception - or add - behavior dependent 
        // upon your need
    }

    if (_cache.Key == variable) {
        return _cache.Value;
    }

    _cache = new KeyValuePair<int, string>(variable, _lookupDictionary[variable]);
    return _cache.Value;
}
privateidictionary\u lookupDictionary;
私钥值对缓存;
公共字符串GetValue(int变量){
if(!\u lookupDictionary.ContainsKey(变量)){
//引发异常或添加行为相关
//根据你的需要
}
if(_cache.Key==变量){
返回_cache.Value;
}
_cache=新的KeyValuePair(变量,_lookupDictionary[variable]);
返回_cache.Value;
}
使用c#8.0,您可以使用新的开关语法:

var area = figure switch 
{
    Line _      => 0,
    Rectangle r => r.Width * r.Height,
    Circle c when c.Radius == 0 => throw new ThrowSomeException(c),
    Circle c    => Math.PI * c.Radius * c.Radius,
    _           => throw new UnknownFigureException(figure)
};

你可以阅读更多关于新功能的信息。

你可以做
返回部分,但不能做
var result=
部分。但是你为什么不试试看呢?我不明白你想做什么。让我们假设你可以做你在第二段代码中正在做的事情。如果你这样做,你将失去结果的价值。这并不意味着什么对我来说没有任何意义。这就是我们使用方法的原因。你需要定义一个返回值的方法,该方法取决于开关的大小写。你有没有检查过
switch
返回值的官方文档?它是说“operator
switch
”还是“method
switch
”?它应该给你一个线索,它是否返回值。如果你需要确切的细节-语言语法是真相的最终来源。如果上述来源中的任何信息不清楚-更新帖子,它将更适合于此。就像格式一样。读起来比缩进嵌套条件好得多。
private IDictionary<int, string> _lookupDictionary;
private KeyValuePair<int, string> _cache; 

public string GetValue(int variable) {
    if (!_lookupDictionary.ContainsKey(variable)) {
        // throw an exception - or add - behavior dependent 
        // upon your need
    }

    if (_cache.Key == variable) {
        return _cache.Value;
    }

    _cache = new KeyValuePair<int, string>(variable, _lookupDictionary[variable]);
    return _cache.Value;
}
var area = figure switch 
{
    Line _      => 0,
    Rectangle r => r.Width * r.Height,
    Circle c when c.Radius == 0 => throw new ThrowSomeException(c),
    Circle c    => Math.PI * c.Radius * c.Radius,
    _           => throw new UnknownFigureException(figure)
};