Jason D
Jason D

Reputation: 2664

Using an object from an ObservableCollection as a property

I have a hypothetical situation regarding objects and properties that I'm hoping somebody can clarify for me. Say I have a class named Employee. Each Employee has an employer, which we'll call Company, and I have an ObservableCollection<Company> called Companies that has been populated.

Employee needs to be able to keep track of the Company that employs it. Supposing I created this property in Employee: public Company Employer { get; set; } and set it with _employee.Employer = Companies[0], would that create a new, redundant instance of Company?

If so, is there a way to reference the appropriate instance of Company that Employee needs from Companies and use that as the value of its Employer property without creating unnecessary objects? What would be the best way to go about this (assuming I'll be creating multiple instances of Employee)?

Any advice would be appreciated.

Upvotes: 1

Views: 49

Answers (1)

user2509738
user2509738

Reputation: 219

This will create a new reference to the same object if the types in question are classes and not structs. This is not generally an expensive operation and will not create new objects. So from what you have said I think it is ok.

Upvotes: 2

Related Questions