Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:11:23+00:00 2026-05-28T14:11:23+00:00

I think those with even a slight grasp on basic string manipulation, loops and

  • 0

I think those with even a slight grasp on basic string manipulation, loops and dictionaries can work out how to populate a Dictionary from a String such as this:

Black:#00000|Green:#008000| (where “Black” is the Key and “#000000” is the Value)

But what is the most ‘elegant’ way of doing it in your opinion? What is the most efficient/more concise coding I can use to achieve it? So far I have:

    public static Dictionary<String, String> ThemeColors
    {
        get
        {
            Dictionary<String, String> themeColors = new Dictionary<string, string>();
            foreach (String colorAndCode in GetSettingByName("ThemeColors").ToString().Split('|'))
            {
                themeColors.Add(colorAndCode.Split(':').First(), colorAndCode.Split(':').Last());
            }
            return themeColors;
        }
    }

GetSettingByName(“ThemeColours”) returns the string above (in Bold).

It’s functional obviously, it all works, but I want to make sure I’m beginning to think beyond this now and working out the best way of doing things rather than just getting it working.

Can I use a yield on the Dictionary loop for example??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:11:24+00:00Added an answer on May 28, 2026 at 2:11 pm

    I actually like a slight variant of your approach best:

        public static Dictionary<String, String> ThemeColors
        {
            get
            {
                Dictionary<String, String> themeColors = new Dictionary<string, string>();
                foreach (String colorAndCode in GetSettingByName("ThemeColors").ToString().Split('|'))
                {
                    var parts = colorAndCode.Split(':');
                    themeColors.Add(parts[0], parts[1]);
                }
                return themeColors;
            }
        }
    

    The only difference is that the second Split() is done only once, and direct indexing is rather than First() and Last().

    Now ToDictionary() is great when it makes sense to include something in a wider query, and I certainly wouldn’t consider it wrong, but it’s not like your approach is particularly verbose or anything.

    But I like that it’s easy to change your approach to tolerate duplicates (use dict[parts[0]] = parts[1] and it’ll over-write duplicates rather than throwing), but it’s also easy to change to throw for Black:#00000:#010101 by testing the size of parts.

    In the other direction, if you’ve a need to get as quick a parsing of a massive string as possible, then throw elegance out the window and replace it with something that scans throught the string tokenising by | rather than using Split().

    Meanwhile, the above fits a nice middle-ground between concision and precision.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this is going to be one of those simple-when-you-see-it problems, but it
It's one of those things I see a lot but never really think of.
I have what I THINK is a simple problem here. I'm calling a basic
I've developed an email application which can be consist of several attachment.size of those
I see all those tutorials about how one can use Javascript for input validating
Can anyone think of a reason why the spacing between the links in the
I really have almost no idea how I can even begin to do this
I ran into this today and the only thing I can think is that
I had my code work fine yesterday, but today I can no longer get
Being new to VS, how may I think of these two concepts, what is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.