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

  • Home
  • SEARCH
  • 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 8272143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:02:30+00:00 2026-06-08T07:02:30+00:00

How do you define a getter and setter for complex data types such as

  • 0

How do you define a getter and setter for complex data types such as a dictionary?

public Dictionary<string, string> Users
{
    get
    {
        return m_Users;
    }

    set
    {
        m_Users = value;
    }
}

This returns the entire dictionary? Can you write the setter to look and see if a specific key-value pair exists and then if it doesn’t, add it. Else update the current key value pair? For the get, can you return a specific key-value pair instead of the whole dictionary?

  • 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-08T07:02:31+00:00Added an answer on June 8, 2026 at 7:02 am

    It is not possible to do it in a way that would involve only properties. You theoretically could write a setter, but for a getter, you would need to specify a key that you want to retrieve. That is impossible since properties do not accept parameters. Natural way to accomplish what you want would be to use methods:

    private Dictionary<string, string> users = new Dictionary<string, string>();
    
    public void Set(string key, string value)
    {
        if (users.ContainsKey(key))
        {
            users[key] = value;
        }
        else
        {
            users.Add(key, value);
        }
    }
    
    public string Get(string key)
    {
        string result = null;
    
        if (users.ContainsKey(key))
        {
            result = users[key];
        }
    
        return result;
    }
    

    Alternatively, as others have already said, you could use indexers, but I’ve always found them a little cumbersome. But I guess it’s just a matter of personal preference.

    And just for the sake of completeness, this is how a setter could look like, although it’s highly unusual and counter-intuitive to have such a property:

    public KeyValuePair<string, string> Users
    {
        set
        {
            Set(value.Key, value.Value);
        }
    }
    

    Internally, it uses the Set method from my previous snippet.

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

Sidebar

Related Questions

my Getter/Setter methods check the value, before they set/return it. When the value is
What is the simplest way to define setter and getter in Python? Is there
I am trying to define simple getter/setter methods for a mixin class that I
I have a class defined like this, with the appropriate getter and setter methods...
In C#, it is possible to define a readonly getter function by not defining
// MACROS #define A_PROPERTY(TYPE, NAME) \ private: \ TYPE NAME; \ public: \ void
Possible Duplicate: error: writable atomic property cannot pair a synthesized setter/getter with a user
Possible Duplicate: error: writable atomic property cannot pair a synthesized setter/getter with a user
I want to create getter/setter methods dyanmically to retrieve private properties. This is what
So I've read that you're supposed to access object attributes through getter/setter methods like

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.