6

I have been trying to create a class which has a property for key value pair, I have tried the Dictionary keyword, but I need something like this:

ClassName clsName = new ClassName(); clsName.PropertyName.Add["Key"] = value; 

I want it to be dynamic property so I can send any datatype.

3
  • 4
    Typically you'd talk directly to the class or property (clsName["Key"] = value, etc.), and not through an Add. As such, I'm not completely sure I understand what you're hoping to do. Do you just need to make an indexer on the class? Commented Apr 1, 2015 at 6:03
  • unlike javascript/python or other language, .NET is mostly strongly type thus made dynamic properties more difficult, however you can try to implement indexer in one of your property, however due to the nature of strongly type, the indexer might return object which you need to cast to other type Commented Apr 1, 2015 at 6:09
  • Its a little unclear what you are asking here. Do you want what essentially a Dictionary provides you but allows any type to be stored in the Value? If so than us object as the Value of your dictionary, so use a Dictionary<string,object> and not a custom class. Are you trying to make a class that stores a representation of other classes? Then PropertyName's type should be Dictionary<string,object>. Perhaps if you can show us some examples of how you would like to use/address/interact with this hypothetical class, we can show you how its made. Commented Sep 14, 2023 at 19:53

5 Answers 5

9

If we suppose that KeyValuePair has as a key as a string, and a value as int, then you could try this:

clsName.PropertyName = new KeyValuePair<string, int>("keyName", 2);

You don't need to use the Add method. Actually, the latter makes sense when you have a collection and don't want to add an item to it. From what you have posted in your question, we can't say that this is your case.

Sign up to request clarification or add additional context in comments.

Comments

6

I suggest you to simply use the "HASHTABLE" its so much easier for you.Below is syntax.

 Hashtable hashtable = new Hashtable(); hashtable.Add("Area", 1000); hashtable.Add("Perimeter", 55); 

1st parameter represents the key and 2nd one represents the value.So its the key value pair.

1 Comment

Link to official documentation for C# Hashtable Class
3

If you are after a basic class, for key and value, would

KeyValuePair<string, object> 

work for you?

Comments

1
public class ClassName { public KeyValuePair<string, object> PropertyName {get; set; } } var c = new ClassName(); c.PropertyName = new KeyValuePair<string, object>("keyName", someValue); 

or, if you need to store multiple values, use Dictionary<string, object> as type of your property.

public class ClassName { public ClassName() { this.PropertyName = new Dictionary<string, object>(); } public Dictionary<string, object> PropertyName {get; set; } } var c = new ClassName(); c.PropertyName.Add("stringKey", anyValue); 

Comments

1

I'm not sure if I understood the question correctly, but apparently your requirements can be met using a generic Dictionary, where the key type parameter is string and the value type parameter is object, i.e. you could use Dictionary<string,object> like this:

public class ClassName { public Dictionary<string, object> Dictionary { get; set; } } 

And then:

ClassName classObject = new ClassName(); classObject.Dictionary.Add("Key", new { "value" }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.