Tuesday, 15 January 2013

c# 4.0 - simple bitwise operation for three state variable -



c# 4.0 - simple bitwise operation for three state variable -

i have object can has 3 different state variables boolean. rule 1 of 3 can true @ 1 time. others must false.

i'm writing server-side validation , need confirm rule. there simple operation can utilize test 1 state beingness true?

i prefer avoid triple if logic this.

update: not in asp.net. record beingness passed 3 booleans. object passed via json serialization. i'm working given me.

if object has 3 bool properties , 1 should true, approach:

public static int truecount(params bool[] booleans) { homecoming booleans.count(b => b); }

then it's simple:

bool onlyonetrue = truecount(obj.pprop1, obj.prop2, obj.prop3) == 1;

demo

note enumerable.count needs using system.linq;

c#-4.0 logic

No comments:

Post a Comment