Want to create a custom class of type Dictionary<int, T>
Solution 1
The problem is with your declaration. Your custom class only needs a single type parameter, since the int
type never varies:
public class MyDictionary<T> : Dictionary<int, T>
{
public string Name { get; set; }
}
Solution 2
We can create our custom dictionary class :
public class Dict<TKey, UValue> : IEnumerable<KeyValuePair<TKey, UValue>>
{
private LinkedList<KeyValuePair<TKey, UValue>>[] _values;
private int capacity;
public Dict()
{
_values = new LinkedList<KeyValuePair<TKey, UValue>>[15];
}
public int Count => _values.Length;
public void Add(TKey key,UValue val)
{
var hash = GetHashValue(key);
if(_values[hash]==null)
{
_values[hash] = new LinkedList<KeyValuePair<TKey, UValue>>();
}
var keyPresent = _values[hash].Any(p => p.Key.Equals(key));
if(keyPresent)
{
throw new Exception("Duplicate key has been found");
}
var newValue = new KeyValuePair<TKey, UValue>(key, val);
_values[hash].AddLast(newValue);
capacity++;
if(Count<= capacity)
{
ResizeCollection();
}
}
private void ResizeCollection()
{
throw new NotImplementedException();
}
public bool ContainsKey(TKey key)
{
var hash = GetHashValue(key);
return _values[hash] == null ? false : _values[hash].Any(p =>
p.Key.Equals(key));
}
public UValue GetValue(TKey key)
{
var hash = GetHashValue(key);
return _values[hash] == null ? default(UValue) :
_values[hash].First(m => m.Key.Equals(key)).Value;
}
public IEnumerator<KeyValuePair<TKey, UValue>> GetEnumerator()
{
return (from collections in _values
where collections != null
from item in collections
select item).GetEnumerator();
}
private int GetHashValue(TKey key)
{
return (Math.Abs(key.GetHashCode())) % _values.Length;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public UValue this[TKey key]
{
get
{
int h = GetHashValue(key);
if (_values[h] == null) throw new KeyNotFoundException("Keys not found");
return _values[h].FirstOrDefault(p=>p.Key.Equals(key)).Value;
}
set
{
int h = GetHashValue(key);
_values[h] = new LinkedList<KeyValuePair<TKey, UValue>>();
_values[h].AddLast(new KeyValuePair<TKey, UValue>
(key,value));
}
}
}
static void Main(string[] args)
{
Dict<int, int> dict=new Dict<int, int>();
dict.Add(-1, -1);
dict.Add(1, -1);
dict.Add(9, -1);
dict.ContainsKey(9);
dict.GetValue(9);
dict[10] = 34;
foreach(var item in dict)
{`enter code here`
Console.WriteLine(item.Key);
Console.WriteLine(item.Value);
}
Console.WriteLine(dict[10]);
}
mrblah
test testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest test asdf asdf
Updated on September 21, 2020Comments
-
mrblah over 3 years
I want to create a custom class that basically wraps a dictionary.
I want to add a property to it called Name.
I tried:
public class MyDictionary<int, T> : Dictionary<int, T> { public string Name { get; set;} }
Doesn't seem to be working, any ideas?
Update
THe error I'm getting is:
Type parameter declaration must be an identifier not a type
-
Faither over 5 yearsprobably because checked rules of stuckoverflow and figured out that answers including only code might be not popular here.
-
John Saunders over 5 years@V.7 is that better?
-
DCCoder over 3 yearsGenerally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.