I'm creating a class which populates a dictionary as a private member. I want to expose the values of the dictionary without exposing the dictionary itself (in a read-only fashion.) I can easily make a property to expose _dictionary.Keys
, but how can I overload []
so that MyClass[key]
returns _dictionary[key]
?
I think I may need to implement an interface, but I'm not sure which one...
In the following example, a class called complx is defined to model complex numbers, and the + (plus) operator is redefined in this class to add two complex numbers. where () is the function call operator and [] is the subscript operator. You cannot overload the following operators: .
Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes).
In case of operator overloading all parameters must be of the different type than the class or struct that declares the operator. Method overloading is used to create several methods with the same name that performs similar tasks on similar data types.
You overload a function name f by declaring more than one function with the name f in the same scope. The declarations of f must differ from each other by the types and/or the number of arguments in the argument list.
Many operators can be overloaded in a different way: as a member function. Overloading operators using a member function is very similar to overloading operators using a friend function. When overloading an operator using a member function: The overloaded operator must be added as a member function of the left operand.
It is achievable because ‘+’ operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading . Attention geek!
Unary operators are usually overloaded as member functions as well, since the member version has no parameters. The following rules of thumb can help you determine which form is best for a given situation: If you’re overloading assignment (=), subscript ( []), function call ( ()), or member selection (->), do so as a member function.
1 For operator overloading to work, at least one of the operands must be a user defined class object. 2 Assignment Operator: Compiler automatically creates a default assignment operator with every class. ... 3 Conversion Operator: We can also write conversion operators that can be used to convert one type to another type. ... More items...
I'm pretty sure this is what you're after:
public TValue this[TKey key] {
get { return _dictionary[key]; }
}
If you want to implement an interface to indicate to client code that your class can be accessed by an index of type TKey
, the closest match (that I'm aware of) is IDictionary<TKey, TValue>
.
Unfortunately, IDictionary<TKey, TValue>
has a whole bunch of members that violate your read-only requirement, which means you would have to explicitly implement lots of members only to throw a NotImplementedException
(or somesuch) when they're called: namely, the setter for this
, Add
, Clear
, and Remove
.
Maybe there's a different interface that would be more appropriate for this purpose (something like IReadOnlyDictionary<TKey, TValue>
?); I just haven't come across it.
You could also write your own interface, of course, if you intend to have multiple classes that offer functionality similar to this.
This is called an Indexer. See http://msdn.microsoft.com/en-us/library/2549tw02.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With