BookID with an integer field IdBook with a string property TitleLibrary that stores books in a dictionary with BookID as keyLibrary to get and set Book objects by BookIDBookID with an integer field IdBook with a string property TitleLibrary that stores books in a dictionary with BookID as keyLibrary to get and set Book objects by BookIDBookID struct and Book classBookID with a public integer field Id. Also create a class called Book with a public string property Title that has get and set accessors.Think of BookID as a simple label for books. The Book class holds the book's title.
Library class with a dictionary to store booksLibrary. Inside it, create a private dictionary called books that uses BookID as key and Book as value. Initialize books in the constructor.The dictionary will help you store and find books by their BookID.
Library to get and set books by BookIDLibrary class, add a public indexer that takes a BookID parameter and returns a Book. The indexer should get the book from the books dictionary if it exists, or return null if not. It should also allow setting a book for a given BookID.The indexer lets you write library[bookId] to get or set a book.
Library object called library. Add two books with BookID values 1 and 2 and titles "C# Basics" and "Advanced C#" using the indexer. Then print the titles of these books using the indexer.Use the indexer to add and then print book titles by their BookID.
What is the main purpose of an indexer in a C# class?
Choose the best description.
Which of the following is the correct syntax to declare an indexer in a C# class using a custom type Key as the index?
public class MyCollection {
// Indexer declaration here
}this followed by square brackets with the parameter type and name inside.this[Key key] with get and set accessors. Others have syntax errors or wrong brackets.Given the following code, what will be the output?
public record Key(int Id);
public class Store {
private Dictionary<Key, string> data = new();
public string this[Key key] {
get => data.TryGetValue(key, out var val) ? val : "Not found";
set => data[key] = value;
}
}
var store = new Store();
store[new Key(1)] = "Apple";
store[new Key(2)] = "Banana";
Console.WriteLine(store[new Key(1)]);
Console.WriteLine(store[new Key(3)]);Identify the error in the following code snippet that implements an indexer with a custom type Key:
public class DataStore {
private Dictionary<Key, int> dict = new();
public int this[Key key] {
get { return dict[key]; }
set { dict.Add(key, value); }
}
}You want to create a class Matrix that uses a custom struct Position as the indexer key to store integer values. The Position struct has two int fields: Row and Col. Which approach correctly implements the indexer to allow getting and setting values by Position?
public struct Position {
public int Row, Col;
public Position(int r, int c) { Row = r; Col = c; }
}
public class Matrix {
private Dictionary<Position, int> values = new();
// Implement indexer here
}Pass the test and this pattern moves unknown