Freddy.
Freddy.

Reputation: 1745

A more optimise way to iterate through JToken properties?

I am iterating through JToken properties and if a key value equal a certain string then it will do a specific action.

foreach (JToken type in typeList)
                            {

                                if (type["type"].Value<string>() == "Car")
                                {
                                    Do Something...
                                }
                                else if (type["type"].Value<string>() == "Truck")
                                {
                                    Do Something...
                                } ....

Is there a better way to write this as a have a fair few different object types.

Upvotes: 0

Views: 164

Answers (1)

Adrian
Adrian

Reputation: 8597

A switch statement perhaps... because in this case a switch statement would be more effective because it would stop when it hits the "found" value rather than go through all the ifs comparisons unless you return in them, then I doubt you'll make anything much efficient - not noticeably for sure.

switch(type["type"].Value<string>())
{
    case "Car":
       Do Something...
       break; // (or ) reaturn xxx (or ) go to xxx.

}

Upvotes: 2

Related Questions