code 1
I have Argument Null Exception at that code in this portion
Code 2
Are they just same function of code 1?
these are all from public source but I just need to know if this two is good or not..
PHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System
{
public class SafeDictionary<T1, T2>
{
private Dictionary<T1, T2> DictBase;
public SafeDictionary(int capacity)
{
DictBase = new Dictionary<T1, T2>(capacity);
}
public SafeDictionary()
{
DictBase = new Dictionary<T1, T2>();
}
public Dictionary<T1, T2> Base
{
get
{
return DictBase;
}
}
public int Count
{
get
{
return DictBase.Count;
}
}
public bool Add(T1 key, T2 value)
{
if (!DictBase.ContainsKey(key))
{
DictBase.Add(key, value);
return true;
}
return false;
}
public void Remove(T1 key)
{
DictBase.Remove(key);
}
public bool ContainsKey(T1 key)
{
return DictBase.ContainsKey(key);
}
public bool ContainsValue(T2 value)
{
return DictBase.ContainsValue(value);
}
public void Clear()
{
DictBase.Clear();
}
public T2 this[T1 key]
{
get
{
if (ContainsKey(key))
return DictBase[key];
else return default(T2);
}
}
public Dictionary<T1, T2>.ValueCollection Values
{
get
{
if (DictBase == null)
DictBase = new Dictionary<T1, T2>();
return DictBase.Values;
}
}
public bool TryGetValue(T1 key, out T2 value)
{
return DictBase.TryGetValue(key, out value);
}
/*internal void Add(ushort p, Conquer_Online_Server.Interfaces.IProf Prof)
{
throw new NotImplementedException();
} */
}
}
PHP Code:
public bool ContainsKey(T1 key)
{
return DictBase.ContainsKey(key);
}
Are they just same function of code 1?
PHP Code:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace System.Collections.Generic
{
public class ThreadSafeDictionary<T1, T2>
{
private Dictionary<T1, T2> SafeDictionaryBase;
public T2[] Values = new T2[0];
public ThreadSafeDictionary(int capacity)
{
SafeDictionaryBase = new Dictionary<T1, T2>(capacity * 10);
}
public int Count
{
get
{
return SafeDictionaryBase.Count;
}
}
public Dictionary<T1, T2> Base
{
get
{
return SafeDictionaryBase;
}
}
public void Add(T1 key, T2 value)
{
if (SafeDictionaryBase.ContainsKey(key) == false)
{
lock (SafeDictionaryBase)
SafeDictionaryBase.Add(key, value);
safeUpdate();
}
}
public void Remove(T1 key)
{
lock (SafeDictionaryBase)
SafeDictionaryBase.Remove(key);
safeUpdate();
}
public T2 this[T1 key]
{
get
{
if (ContainsKey(key))
return SafeDictionaryBase[key];
else return default(T2);
}
}
public bool TryGetValue(T1 key, out T2 value)
{
return SafeDictionaryBase.TryGetValue(key, out value);
}
public bool ContainsKey(T1 key)
{
return SafeDictionaryBase.ContainsKey(key);
}
public bool ContainsValue(T2 value)
{
return SafeDictionaryBase.ContainsValue(value);
}
private void safeUpdate()
{
if (Values == null)
{
Values = SafeDictionaryBase.Values.ToArray();
}
else
{
if (System.Threading.Monitor.TryEnter(Values, 1))
{
Values = SafeDictionaryBase.Values.ToArray();
}
}
}
}
}