Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently using this to convert a List of booleans to a string

    var tempbllist = new List<int>();
        foreach (var t in blenabled)
            switch (t)
                case true:
                case false:

        Settings.Default.blEnabled = String.Join(",",
            tempbllist.Select(i => i.ToString(CultureInfo.InvariantCulture)).ToList());

(converts them to 1s and 0s to save space)

and this to convert them back

if (Settings.Default.blEnabled.Contains(","))
            blenabled =
                new List<bool>(
                    Settings.Default.blEnabled.Split(',').Select(Int32.Parse).Select(i => i == 1).ToList());

What is a better way of doing this?

share|improve this question
When you say 'save space' do you mean in characters in a file or memory? If you mean the latter you'd be misteken, as an int (actually System.Int32) takes you 4 bytes whereas a bool (System.Boolean) only uses 1 byte. Reference: – Mark Aug 21 '14 at 6:10
up vote 8 down vote accepted

How about this:

To Save:

Settings.Default.blEnabled = string.Join(",", blenabled.Select(x=>x?"1":"0"));

To Load:

  blenabled = Settings.Default.blEnabled

Is there some requirement that you save them as integers? It could be further simplified by just using bool.ToString() and bool.Parse()...

share|improve this answer
I convert them to integers to save a bit of space, since there is up to 96 different values. It's not much, but I figure it helps. :P – user51552 Aug 21 '14 at 1:12
The order of arguments to string.Join is reversed. You can also just write string.Join(",", bs.Select(b => b ? '1' : '0')). – mjolka Aug 21 '14 at 4:42
Good catch @mjolka - I updated the answer with your suggestions / fix... – Matt Murrell Aug 21 '14 at 4:52
.Select(x=> x == '1') – NPSF3000 Aug 21 '14 at 9:33
@NPSF3000, it should be "1", not '1' since you have a string[] – TopinFrassi Aug 21 '14 at 13:42
  • tempbllist? Why are we dsmvwlng? I'm sure you can find something better in the greater context of your code, but lacking that context, I can at least suggest boolList instead.
  • Seriously, what's with these variable names? What's a blen-a-bled? Ohhhh you mean boolEnabled? But it's an enumerable of some kind, wouldn't if be more clear if it was plural? Maybe... enabledBools instead?

        foreach (var t in blenabled)
  • I almost forgot t.... Single letter variable names are frustrating. It's worse when people use t because it's an awful lot like <T>.

Yeah. That was harsh. I know and I'm sorry that I seem to have lost my filter for a moment. Naming is hard. Really, really hard, but it's also incredibly important. Names in the code tell other devs what you were thinking when you wrote the code. They're better than comments when it's done right. Heck, good names replace comments when it's done right. This is not naming done right.

Ok. I'm done picking on the naming. I've just one more axe to grind.

  • Switches are great. Seriously, I think they're the best thing since peanut butter and pickle sandwiches, but they're not always the right tool for the job. We've got more than a hammer and not every problem is a nail. A simple If...Else is perfect here. Why? Because there will only ever be two paths. It's true or it's false. There will never be another case.
share|improve this answer
when the name of blenabled is changed to a enabledBools then instead of t the variable should be enabledBool because that makes sense. – Malachi Aug 22 '14 at 13:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.