Prev: Events and Dispose
Next: WPF Message queue
From: Tony Johansson on 24 Jun 2010 19:51 Hello! Assume I have this class declaration public class EmployeeCollection : IDictionary <int, Employee> { // Implementation code } I assume that I can't use this code because I must add an int as the first parameter and an Employee as the second parameter. In this example I add the object KeyValuePair which should not be valid because we have this declaration IDictionary <int, Employee> Employee e1, e2; e1 = new Employee (1001, "Andy Reid", "Manager"); e2 = new Employee (1002, "Kara Lang", "Sales Engineer"); EmployeeCollection eData = new EmployeeCollection(); eData.Add (new KeyValuePair <int, Employee> (e1.ID, e1)); eData.Add (new KeyValuePair <int, Employee> (e2.ID, e2)); This code must be valid because here I add an int as the first parameter and an Employee as the second parameter. Employee e1, e2; e1 = new Employee (1001, "Andy Reid", "Manager"); e2 = new Employee (1002, "Kara Lang", "Sales Engineer"); EmployeeCollection eData = new EmployeeCollection(); eData.Add (e1.ID, e1); eData.Add (e2.ID, e2); Is my conclusion right about the two examples ? //Tony
From: Arne Vajhøj on 24 Jun 2010 20:15 On 24-06-2010 19:51, Tony Johansson wrote: > Assume I have this class declaration > > public class EmployeeCollection : IDictionary<int, Employee> > { > // Implementation code > } > > I assume that I can't use this code because I must add an int as the first > parameter and an Employee as the second parameter. In this example I add the > object KeyValuePair which should not be valid because > we have this declaration IDictionary<int, Employee> > > Employee e1, e2; > e1 = new Employee (1001, "Andy Reid", "Manager"); > e2 = new Employee (1002, "Kara Lang", "Sales Engineer"); > EmployeeCollection eData = new EmployeeCollection(); > eData.Add (new KeyValuePair<int, Employee> (e1.ID, e1)); > eData.Add (new KeyValuePair<int, Employee> (e2.ID, e2)); > > This code must be valid because here I add an int as the first parameter and > an Employee as the second parameter. > > Employee e1, e2; > e1 = new Employee (1001, "Andy Reid", "Manager"); > e2 = new Employee (1002, "Kara Lang", "Sales Engineer"); > EmployeeCollection eData = new EmployeeCollection(); > eData.Add (e1.ID, e1); > eData.Add (e2.ID, e2); > > Is my conclusion right about the two examples ? Your conclusions looks correct to me. You can easily check by copy pasting the code into the a source file and ask the C# compiler if it also agrees! :-) I would prefer to make the class: public class EmployeeCollection { private Dictionary<int, Employee> data; .... public void Add(Employee e) { data.Add(e.ID, e); } .... public Employee Get(int id) { return data.get(id); } .... } because it provides better encapsulation. But that is OOP not C# as such. Arne
From: Peter Duniho on 24 Jun 2010 23:47 Tony Johansson wrote: > Hello! > > Assume I have this class declaration > > public class EmployeeCollection : IDictionary <int, Employee> > { > // Implementation code > } > > I assume that I can't use this code because I must add an int as the first > parameter and an Employee as the second parameter. In this example I add the > object KeyValuePair which should not be valid because > we have this declaration IDictionary <int, Employee> IDictionary<TKey, TValue> inherits from ICollection<KeyValuePair<TKey, TValue>>, and ICollection<T> requires an Add(T item) method. So, your assumption is incorrect. Your EmployeeCollection class is required to have the Add(KeyValuePair<int, Employee>) method, and so the code you posted in the first example should work just fine. However, it is a great mystery to me why you would not simply just test your theory with the compiler as Arne suggests. What is the point of asking your question here, when you've got the tools right in front of you (or should have, anyway) to get the question answered more quickly and more accurately. > [...] > Is my conclusion right about the two examples ? No. While it's true that your EmployeeCollection also has an Add(int, Employee) overload, it is required to have the Add(KeyValuePair<int, Employee>) overload as well. Both code examples should work fine (at least as far as the Add() methods go). Pete
|
Pages: 1 Prev: Events and Dispose Next: WPF Message queue |