KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Assume you have a basic Employee class as such: class Employee { public string Name; public int Years; public string Department; } Then (in a seperate class) I have the following code fragments (I think i understand all but the last): I believe that the following code fragment works because the array initiliser creates an array of Employee objects which are the same type as the workforce variable being assigned to. Employee[] workforceOne = new Employee[] { new Employee() { Name = "David", Years = 0, Department = "software" }, new Employee() { Name = "Dexter", Years = 3, Department = "software" }, new Employee() { Name = "Paul", Years = 4, Department = "software" } }; I then have the following code fragment. I believe this works because the array of Employee objects implicity is an implementation of the Array() class which implements IEnumerable . Therefore, I believe this is why the array can be assigned to IEnumerable? IEnumerable workforceTwo = new Employee[] { new Employee() { Name = "David", Years = 0, Department = "software" }, new Employee() { Name = "Dexter", Years = 3, Department = "software" }, new Employee() { Name = "Paul", Years = 4, Department = "software" } }; Then I have this code fragment: IEnumerable<Employee> workforceThree = new Employee[] { new Employee() { Name = "David", Years = 0, Department = "software" }, new Employee() { Name = "Dexter", Years = 3, Department = "software" }, new Employee() { Name = "Paul", Years = 4, Department = "software" } }; I am not sure why this code fragment works? IEnumerable<Employee> inherits from IEnumerable (and overrides (or overloads?) the GetEnumerator() method) but shouldn't i therefore need a cast for the above to work
Tags (comma-separated)
Save Edits
Cancel