I have the following:
if (model.PartitionKey.Substring(2, 2) == "05" ||
model.PartitionKey.Substring(2, 2) == "06")
I have more like this. Is there a more clean way to code this where I don't have to repeat model.PartitionKey twice ?
I have the following:
if (model.PartitionKey.Substring(2, 2) == "05" ||
model.PartitionKey.Substring(2, 2) == "06")
I have more like this. Is there a more clean way to code this where I don't have to repeat model.PartitionKey twice ?
if (new []{"05", "06"}.Contains(model.PartitionKey.Substring(2, 2))
the syntax might be far off, corrections are welcome :)
Edit:
new []
For such kind of cases I use an Extension Method
and call it as
Being an Extension Method we can call it for all types like
OR
What about this:
That leaves you at liberty to keep the strings you are looking for in a nice list...
This will help a lot if the list of strings you are looking for gets longer than just two... Also, you can then add this to a set (
HashSet<string>
) for more efficient lookup - but test this first, as overhead can eat up gains.To aid readibility you could extract the
Substring
out into a variable and then test that:But otherwise that is about it.
I'm surprised nobody offered switch as a possible alternative :)
You can read more about it at MSDN