可以从 C# 中的 switch case 分配 return 吗?
Possible to assign a return from a switch case in C#?
我见过的所有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>;
}
可能吗? (也许我在箱子里需要 break
,但它是 return...)
return <somevalue>
背后的基本思想是正确的,但 switch
是一个 控制语句 ,因此它 return
不是一个值。你必须让它成为这样的方法:
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
}
在这种情况下我还需要解释一下:方法不能return
隐式类型(var
), 因为编译器无法猜测 return
type 是什么。但是你可以 return
dynamic and now type will be changed runtime. Also you can't use dynamic
in switch
because it requires a nullable 输入。
如果你想要它简短(在方法中),可以用 lambda 创建一个匿名方法:)
var result =
(Func<int, dynamic>)
( (x) =>
{
switch(x)
{
case 1: return "text";
case 2: return 5;
default: return null;
}
} // Lambda
) // Func<int, dynamic> (takes int parameters, returns dynamic value)
(); // Call it and get return value to assign
但是我强烈建议您阅读一些文章,例如 statements, methods, types...
var result =
variable == 1? <somevalue>:
variable == 2? <someothervalue>:
<defaultvalue>;
如果您想要 return 基于某些输入的值,并且您希望将开关作为可能的解决方案,请考虑使用字典。
// 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>;
}
请注意,开关本身没有 return 值。如果您还想保存 return 值,那么只需使用字典的索引器执行查找,存储引用,然后 return。
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;
}
在 c# 8.0 中,您可以使用新的 switch 语法:
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)
};
您可以阅读有关新功能的更多信息 here。
我见过的所有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>;
}
可能吗? (也许我在箱子里需要 break
,但它是 return...)
return <somevalue>
背后的基本思想是正确的,但 switch
是一个 控制语句 ,因此它 return
不是一个值。你必须让它成为这样的方法:
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
}
在这种情况下我还需要解释一下:方法不能return
隐式类型(var
), 因为编译器无法猜测 return
type 是什么。但是你可以 return
dynamic and now type will be changed runtime. Also you can't use dynamic
in switch
because it requires a nullable 输入。
如果你想要它简短(在方法中),可以用 lambda 创建一个匿名方法:)
var result =
(Func<int, dynamic>)
( (x) =>
{
switch(x)
{
case 1: return "text";
case 2: return 5;
default: return null;
}
} // Lambda
) // Func<int, dynamic> (takes int parameters, returns dynamic value)
(); // Call it and get return value to assign
但是我强烈建议您阅读一些文章,例如 statements, methods, types...
var result =
variable == 1? <somevalue>:
variable == 2? <someothervalue>:
<defaultvalue>;
如果您想要 return 基于某些输入的值,并且您希望将开关作为可能的解决方案,请考虑使用字典。
// 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>;
}
请注意,开关本身没有 return 值。如果您还想保存 return 值,那么只需使用字典的索引器执行查找,存储引用,然后 return。
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;
}
在 c# 8.0 中,您可以使用新的 switch 语法:
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)
};
您可以阅读有关新功能的更多信息 here。