Matjaž Mav Matjaž Mav - 3 years ago 137
C# Question

Generic this index property in dictionary

How can I achieve something like this. I don't want method that return casted value, I want use

is this possible?

public class DynamicDictionary : IDictionary<string, object>
public T this<T>[string key] where T : class
object value = null;
TryGetValue(key, out value);
return (T)value;

// ...

Answer Source

No, you can't. Indexers - and properties in general - can't be generic in C#. You'd have to use a method instead, or just cast in the calling code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download