迭代 JToken 属性的更优化方法?

我正在遍历 JToken 属性,如果键值等于某个字符串,那么它将执行特定操作。


foreach (JToken type in typeList)

                            {


                                if (type["type"].Value<string>() == "Car")

                                {

                                    Do Something...

                                }

                                else if (type["type"].Value<string>() == "Truck")

                                {

                                    Do Something...

                                } ....

有没有更好的方法来编写它,因为它有很多不同的对象类型。


慕无忌1623718
浏览 139回答 1
1回答

慕森卡

一个 switch 语句也许......因为在这种情况下 switch 语句会更有效,因为它会在达到“找到”值时停止而不是经过所有ifs比较,除非你返回它们,那么我怀疑你会做任何更有效的东西 - 不是很明显。switch(type["type"].Value<string>()){&nbsp; &nbsp; case "Car":&nbsp; &nbsp; &nbsp; &nbsp;Do Something...&nbsp; &nbsp; &nbsp; &nbsp;break; // (or ) reaturn xxx (or ) go to xxx.}
打开App,查看更多内容
随时随地看视频慕课网APP