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 3985846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:56:25+00:00 2026-05-20T05:56:25+00:00

I’m an experienced PHP developer transitioning to C#. At present I am working on

  • 0

I’m an experienced PHP developer transitioning to C#. At present I am working on a Windows Forms application.

I found in my searches that C# doesn’t support associative arrays in the same loose fashion PHP does. I have found info on Dictionary and something about “structs” which seem to be class objects.

The trouble I am having is getting my head around not only an Associative array, but a multi dimensional one that I want to use for keeping multiple counts in a series of loops.

The application is reading a text log file, searching for a predefined string, pulling out the date on that line when the string is found, and incrementing a count for that string match on that date.

In PHP, it would be as easy as this:

// Initialize
$count_array[$string_date][$string_keyword] = 0;

...

// if string is found
$count_array[$string_date][$string_keyword] += 1;

...

// To ouput contents of array
foreach($count_array as $date -> $keyword_count_array) {
    echo $date; // output date

    foreach($keyword_count_array as $keyword -> $count) {
        echo $keyword . ": " . $count;
    }
}

It seems to be a little more involved in C# (which isn’t a bad thing). I have tried using an suggestion I found on another similar question but I don’t really follow how to either increment or iterate/output the contents:

// Initialize
var count_array = new Dictionary<string, Dictionary<string, int>>();
count_array = null;

...

// if string is found - I think the second reference is supposed to be a Dictionary object??
count_array[string_date.ToShortDateString()][string_keyword]++;

...

// To ouput contents of "array"
foreach (KeyValuePair<string, Dictionary<string, int>> kvp in exportArray)
{
    foreach(KeyValuePair<string, int> kvp2 in kvp.Value) 
    {
        MessageBox.Show(kvp.Key + " - " + kvp2.Key + " = " + kvp2.Value);
    }
}

Am I even on the right track? Or does someone have a better/cleaner method of mimicing the PHP code above?

UPDATE

With the above C# code, I actually get an error at the “// if string is found ” line. The error is “Object reference is not set to an instance of an object”. I am assuming that it is because I have a string in the secound reference, not a Dictionary object. So right now, I am unsure how to increment.

UPDATE 2

Thanks everyone for your time. Current code is now functional thanks to understanding how Dictionary’s work. However all advice regarding the use of classes and objects for this situation is not lost either. I may refactor to suit.

  • 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-20T05:56:25+00:00Added an answer on May 20, 2026 at 5:56 am

    The code itself looks sound, the only thing I see missing is there are no checks to see if the values exist before incrementing them.

    Before you call

    count_array[string_date.ToShortDateString()][string_keyword]++;
    

    You’ll need to do:

    string shortDate = string_date.ToShortDateString();
    if (!count_array.ContainsKey(shortDate))
    {
        count_array.Add(shortDate, new Dictionary<string, int>());
    }
    
    if (!count_array[shortDate].ContainsKey(string_keyword))
    {
        count_array[shortDate].Add(string_keyword, 0);
    }
    

    Before you try incrementing anything.

    You need to initialize your dictionary entries by calling .Add or [“key”] = value. Calling ++ on an uninitialized dictionary entry won’t work. Depending on what exactly it is you’re trying to accomplish though it might be a good idea to use a class.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.