The Reference ById method in C# is used to fetch a reference to a particular item in a collection based on its unique identifier. This method is very helpful when we have to access a specific item from a collection of items without iterating through the collection.
Example 1:
Suppose we have a List of Person objects and every person has a unique ID. Now, we want to get a person object for a specific ID.
List people = new List(); people.Add(new Person() { ID = 1, Name = "John" }); people.Add(new Person() { ID = 2, Name = "Jane" }); people.Add(new Person() { ID = 3, Name = "Mike" });
Person person = people.FindById(2); // This will return the person object with ID 2.
Example 2:
Suppose we have an ObservableCollection of Car objects and every car object has a unique registration number. Now, we want to get a Car object for a specific registration number.
ObservableCollection cars = new ObservableCollection(); cars.Add(new Car() { RegistrationNumber = "ABC123", Make = "Honda" }); cars.Add(new Car() { RegistrationNumber = "DEF456", Make = "Toyota" }); cars.Add(new Car() { RegistrationNumber = "GHI789", Make = "BMW" });
Car car = cars.FindById("DEF456"); // This will return the car object with registration number "DEF456".
The package library for the Reference ById method is part of the .NET Framework. Specifically, it is contained within the System.Linq namespace.
C# (CSharp) Reference.ById - 30 examples found. These are the top rated real world C# (CSharp) examples of Reference.ById from package code extracted from open source projects. You can rate examples to help us improve the quality of examples.