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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:38:42+00:00 2026-06-09T01:38:42+00:00

Something like this: Dictionary<int, string> myData = new Dictionary<int, string>(); myData.Add(1, England); myData.Add(2, Canada);

  • 0

Something like this:

Dictionary<int, string> myData = new Dictionary<int, string>();
myData.Add(1, "England");
myData.Add(2, "Canada");
myData.Add(3, "Australia");
myTreeView.Node[0].Tag = myData;

Then I want to get this object, how should I do it ?
Like:

string str = new string();
str = myTreeView.Node[0].Tag[2]; // "str" should be equal to "Canada"
myTreeView.Node[0].Tag[1] = "Spain";
str = myTreeView.Node[0].Tag[1]; // now "str" is equal to "Spain"

Second question – what will return this expression:

Dictionary<int, string> myData = new Dictionary<int, string>();
myData.Add(1, "England");
myData.Add(2, "Canada");
myData.Add(3, "Australia");

string str1 = new string();
str = myData[4]; // there isn't such a key as 4

Exception or null ?

  • 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-06-09T01:38:44+00:00Added an answer on June 9, 2026 at 1:38 am

    Control.Tag is typed as object so you’ll need to cast it to access it as a Dictionary<int, string>:

    Dictionary<int, string> dict = (Dictionary<int, string>)myTreeView.Node[0].Tag;
    string str = dict[2];
    

    And similarly to set a value:

    var dict = (Dictionary<int, string>)myTreeView.Node[0].Tag;
    dict[1] = "Spain";
    

    If you try to access a non-existent key, a KeyNotFoundException will be thrown. You can check if the dictionary contains a given key using TryGetValue or ContainsKey:

    if(dict.ContainsKey(key))
    {
        var value = dict[key];
    }
    else
    {
    }
    

    TryGetValue does the lookup and sets the given variable to the value (it it exists) in a single call, so is usually preferred.

    string value;
    if(dict.TryGetValue(key, out value))
    {
        //use value
    }
    else { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use something like this: Dictionary<int, string>[] matrix = new Dictionary<int,
I need something resembling the following: Dictionary<string, List<int>, List<DateTime>> = new Dictionary<string, List<int>, List<DateTime>>()
In C++, I can do something like this: int i[10] = new int[10]; int
Getting an InvalidCastException when trying something like this : IEnumerable<object> test = (IEnumerable<object>)new List<KeyValuePair<string,
I have a python dictionary of type defaultdict(list) This dictionary is something like this:
I want to do something like this, I have a dictionary with keys and
Is something like this possible: function FooClass() { var barsArray=new Array(); var index=0; function
In C++ often do something like this: typedef map<int, vector<int> > MyIndexType; Where I
There's an XML scheme which says something like this: <ExtraFields> <ExtraField Type=Int> <Key>Mileage</Key> <Value>500000
In partial view I work with textboxes like this. @model Dictionary<string, string> @Html.TextBox(XYZ, @Model[XYZ])

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.