Complete the code to declare a virtual method named Speak.
public class Animal { public [1] void Speak() { Console.WriteLine("Animal speaks"); } }
The virtual keyword allows a method to be overridden in derived classes, enabling virtual method dispatch.
Complete the code to override the Speak method in the Dog class.
public class Dog : Animal { public [1] void Speak() { Console.WriteLine("Dog barks"); } }
The override keyword is used to provide a new implementation of a virtual method in a derived class.
Fix the error in the method declaration to correctly override the base class method.
public class Cat : Animal { public [1] void Speak() { Console.WriteLine("Cat meows"); } }
To override a virtual method from the base class, the derived class method must use the override keyword.
Fill both blanks to create a dictionary comprehension that maps animal names to their Speak method outputs if the name length is greater than 3.
var sounds = new Dictionary<string, string> {
{"Dog", "Bark"},
{"Cat", "Meow"},
{"Horse", "Neigh"}
};
var filtered = sounds.Where(kv => kv.Key.[1] > 3)
.ToDictionary(kv => kv.Key, kv => kv.[2]);The Length property gets the length of the string key, and Value accesses the dictionary value.
Fill all three blanks to create a dictionary comprehension that maps uppercase animal names to their sounds if the sound length is greater than 3.
var sounds = new Dictionary<string, string> {
{"Dog", "Bark"},
{"Cat", "Meow"},
{"Horse", "Neigh"}
};
var result = sounds.Where(kv => kv.Value.[1] > 3)
.ToDictionary(kv => kv.Key.[2](), kv => kv.[3]);The Length property checks the length of the sound string, ToUpper() converts the key to uppercase, and Value accesses the sound.