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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:46:07+00:00 2026-06-18T20:46:07+00:00

KeyValuePair struct has read-only properties (Key and Value), so I made a custom class

  • 0

KeyValuePair struct has read-only properties (Key and Value), so I made a custom class in order to replace it:

public class XPair<T, U>
{
    // members
    private KeyValuePair<T, U> _pair;

    // constructors
    public XPair()
    {
        _pair = new KeyValuePair<T, U>();
    }
    public XPair(KeyValuePair<T, U> pair)
    {
        _pair = pair;
    }

    // methods
    public KeyValuePair<T, U> pair
    {
        get { return _pair; }
        set { _pair = value; }
    }
    public T key
    {
        get { return _pair.Key; }
        set { _pair = new KeyValuePair<T, U>(value, _pair.Value); }
    }
    public U value
    {
        get { return _pair.Value; }
        set { _pair = new KeyValuePair<T, U>(_pair.Key, value); }
    }
}

Is it possible for this class to also apply to “foreach” usage with Dictionary? Example:

Dictionary<String, Object> dictionary = fillDictionaryWithData();
foreach(XPair<String, Object> pair in dictionary) {
    // do stuff here
}
  • 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-18T20:46:08+00:00Added an answer on June 18, 2026 at 8:46 pm

    That would be possible if your class would implement a conversion operator from KeyValuePair<TKey, TValue>.

    But it still wouldn’t work the way you expect, because changing the key or the value of pair inside the loop will have no effect on the dictionary. Key and value in the dictionary will remain unchanged.

    If you want to change the value inside a dictionary, simply use dictionary[key] = newValue;

    If you want to change a key, I guess that you don’t really want a Dictionary<TKey, TValue>. An IEnumerable<XPair<TKey, TValue>> might be more appropriate.
    If you really need a Dictionary, you can use the following code to “change” a key:

    var value = dictionary[key];
    dictionary.Remove(key);
    dictionary.Add(newKey, value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My struct class: public struct PinAndRadius { public string pinID { get; set; }
I want to store the my custom collection as a Key and Value is
struct mydata { public int id; public string data; } class Program { static
The simplest example would be the built in class keyValuePair(of T,U). I would like
In looking at the source for the KeyValuePair<TKey, TValue> struct, the private member fields
I have an object that has a KeyValuePair type property. I would like to
I'm looking for a KeyValuePair class in Java. Since java.util heavily uses interfaces there
.NET 2 The standard: foreach(KeyValuePair<int,int> entry in MyDic) { entry.Value += i; // does
I've got a MenuManager class to which each module can add a key and
I have a list of keyvaluepair(string,string) first string is something like class.property, second string

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.