-3

As in Java if we declare parameterized constructor of a class as:

public KeyCountMap(Class<? extends Map> _mapType)
{
   _map = _mapType.newInstance();
}  

where _map is defined as:

private Map<T, MutableInt> _map = new LinkedHashMap<T, MutableInt>();  

While in C#, we'll use as:
Type instead of Class<? extends Map>

private Dictionary<T, MutableInt> _map = new Dictionary<T, MutableInt>();  

instead of

private Map<T, MutableInt> _map = new LinkedHashMap<T, MutableInt>();  

EDIT
The actual Java constructor considered is:

@SuppressWarnings({ "unchecked", "rawtypes" })
public KeyCountMap(Class<? extends Map> _mapType) throws InstantiationException, IllegalAccessException 
{
    _map = _mapType.newInstance();
}

As there is no method like newInstance() in C# and I'm not sure about what Activator.CreateInstance() does exactly.

How will we create new instance of object of class Type in C# as done while using java?

maliks
  • 1,102
  • 3
  • 18
  • 42
  • Possible duplicate of [Create instance of generic type?](http://stackoverflow.com/questions/731452/create-instance-of-generic-type) – Christo S. Christov May 25 '16 at 13:32
  • How is this a Java question? – Mr00Anderson May 25 '16 at 13:32
  • @Chris Your suggested duplicate question is talking about parameter less constructor whereas here is a parameterized one. So what ? – maliks May 25 '16 at 13:47
  • @Underbalanced it is related to both `Java` and `C#` as well – maliks May 25 '16 at 13:48
  • @Taufel This isn't Java related in the since your simply asking about C# Generics and Wildcards [What is the equivalent of Java wildcards in C# generics](http://stackoverflow.com/questions/527766/what-is-the-equivalent-of-java-wildcards-in-c-sharp-generics) – Mr00Anderson May 25 '16 at 13:52

1 Answers1

0
public class KeyCountMap<T> 
{

    private readonly IDictionary<T,MutableInt> _map;

    public KeyCountMap(){

        _map = new Dictionary<T,MutableInt>();
    }
}

public class KeyCountMap<T> where T: IDictionary<T, MutableInt>, new()
    {

        private readonly T _map;

        public KeyCountMap()
        {

            _map = new T();
        }
    }

Also remember map in java is an interface IDictionary in c#. Dictionary and Map are not created equal Map[invlaidkey] =null, Dictionary[invalidkey] = exception

EDIT

The second example satisfies the EDIT section. However its not quite equivalent in your example you construct an instance from a type this in c# can be achieved by using the Activator.CreateInstance(). However in c# you have no way to constrain (without writing some reflection) the types passed in. Even then this is a runtime constraint not compile time. So I suggest using generics so the most part; unless you have a particular need to instantiate unknown types at runtime which you have not yet expressed.

Mark
  • 1,544
  • 1
  • 14
  • 26
  • @Mark--well, you initialized `_map` here in this answer, what about `(Class extends Map> _mapType)` in the question? – maliks May 25 '16 at 13:53
  • @Mark--how will we accommodate C# `Type` here, is there any use of it ? – maliks May 25 '16 at 13:59
  • I think so by using `_map = new Dictionary();` we actually creating a new instance. Fine – maliks May 25 '16 at 14:16
  • Type is T in my example and yes you can simply "new it up" ill update my answer with more examples – Mark May 25 '16 at 14:29
  • Yeah I also declared my class as `KeyCountMap` but just was the confusion that will your till suggested answer will cover the constructor as given in my **EDIT** in question – maliks May 25 '16 at 14:33
  • @Mark--what is the basic difference between your 2 suggested example classes? Which one satisfies the **EDIT** section of question? Please also tell what is C# of equivalent of `_map.keyset();` and `_map.entrySet();` ? – maliks May 26 '16 at 13:40
  • _map.Keys _map.Values http://www.dotnetperls.com/dictionary https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx – Mark May 26 '16 at 14:26