List with different types

I am new to C # ... (.net 3.5)

I want the dictionary to contain two different types of objects, one of which is shared. while iterating through the list, I will call methods like add and clone. I tried it with base class and subclasses ....

namespace ConsoleApplication1 { class Element{ } class Child1 : Element { public Child1 Clone() { return clone; } } class Child2<T> : Element { public Child2<T> Clone() { return clone; } } class Program { static void Main(string[] args) { Dictionary<string, Element> d = new Dictionary<string, Element>(); d.Add("c1", new Child1()); d.Add("c2s", new Child2<string>()); d.Add("c2i", new Child2<int>()); foreach (KeyValuePair<string, Element> kvp in d) { Element e = kvp.Value.Clone(); } } } } 

Is there a way or solution for my needs?

Thanks! Anna

+4
source share
4 answers

You can make Clone either abstract or virtual in the base type ( Element ) and override in the derived types, but you cannot change the type of the return value when overriding, so it should be Element (not more specifically). You can reuse methods ( new ...), but it gets messy, and you cannot override and new method with the same name / signature of the same type.

But if you are happy that the return type will be Element ...

 abstract class Element{ public abstract Element Clone(); } class Child1 : Element { public override Element Clone() { return /* your code */; } } class Child2<T> : Element { public override Element Clone() { return /* your code */; } } 
+3
source

Since the .Value type you get from your dictionary is an element, you need to make sure that Element defines all the operations that it should have, for example, your Clone method.

I would:

  • Make Clone virtual and add it to Element (or create abstract elements as well as abstract cloning instead of virtual).
  • Override clone in Child1 and Child2

Thus, the kvp.Value.Clone() code will call the correct Clone method depending on the object returned from the dictionary.

+2
source

Do not create a class hierarchy just to add different words to the same dictionary.

If classes do not have enough decent hierarchical relationships, you are better off using an interface such as ICloneable , which is already available in the .NET framework.

Then just create your dictionary as follows:

 Dictionary<string, ICloneable> d = new Dictionary<string, ICloneable>(); 

It is more flexible. Creating a hierarchy for the sake of generality, the ability to execute Clone () is not the right IMO solution.

0
source

Although I agree with Wim that implementing ICloneable is probably a better solution, rather than trying to implement a nonexistent hierachy class, remember that ICloneable is considered a “bad API” because it does not indicate whether it uses shallow or deep semantics (see ., for example, http://pro-thoughts.blogspot.com/2009/02/write-deep-clone-forget-about.html or do a google search for "ICloneable C # bad API"

0
source

Source: https://habr.com/ru/post/1301893/


All Articles