What type of method can you implement for a C# class that enables you to provide access to internal subitems using an array-like syntax?
An indexer method
What keyword must be used in .NET to declare an anonymous type?
var
What syntax must be used in .NET to declare an anonymous type?
Object initialization syntax
What type of type can be used to quickly model the "shape" of data with little overhead in .NET?
An anonymous type
What type do anonymous types automatically derive from in .NET?
System.Object
What is the syntax for creating an indexer method in C#?
this[]
Example (inside PersonCollection class):
public Person this[int index]
{
get => (Person)arPeople[index];
set => arPeople.Insert(index, value);
}
What is an example of how you might overload the + operator in C# in a Point class?
public static Point operator + (Point p1, Point p2) => new Point(p1.X + p2.X, p1.Y + p2.Y);
What are the first two or three restrictions when defining an extension method in C#?
It must be defined within a static class, and it must also be a static method. The first parameter of the static method must use the this keyword
Example:
static class MyExtensions
{
public static void DisplayDefiningAssembly(this object obj) {}
public static int ReverseDigits(this int i) {}
}
If you had a Square and Rectangle class in a C# app and wanted to be able to write the following code:
Square s = (Square)rectangle
What example code might you have in the Square class?
public static explicit operator Square(Rectangle r)
{
Square s = new Square {Length = r.Height};
return s;
}
What was introduced in .NET to allow adding new methods or properties to a class or structure without modifying the original type in any direct manner?
Extension methods
What is the keyword and syntax used in C# to declare an anonymous type?
var and object initialization syntax
Example:
var car = new { Make = make, Color = color, Speed = currSp };
All anonymous types in C# automatically derive from what class?
System.Object
Previous card
First card
Random order
Next card