Programming Flashcards
Define : Interface
Defines the things that something makes available to the outside world.
Members of an interface are implicitly
public and abstract, and are required to be so.
How class indicates that it implements an interface (code example)
public class AwesomeClass : InterfaceName { implementation of methods}
You can implement multiple interfaces at the same time
True
You can implement interfaces and declare a base class at the same time
True
How interface is defined (code example)
public interface InterfaceName { members }
A class that implements an interface does not have to provide implementation for all of the members of the class
False
A class that implements an interface is allowed to have other members that aren’t defined in the interface
True
A class can have more than one base class
False
Define : Polymorphism
You can create derived classes that implement the same method in different ways
How to implement polymorphism in the base class
mark the method as ‘virtual’
How to implement polymorphism in a derived class
mark it ‘override’ and provide alternative implementation
How to prevent creation of instance from base class
mark the class as ‘abstract’
How to force derived classes to implement methods that don’t exist in base class
In the base class create an empty method marked as abstract
New keyword attached to a method means?
A new method is being created unrelated to any methods in the base class. This is not great.
What are generics?
A way to create type-safe classes and structs without needing to cmmit to a specific data type at design time.
Define a List (code example)
List listofStrings = new List();
The core .Net classes that use generics:
List, Dictionary, IEnumerable
Define Type-Safety
When you always know what type of object you are working with
Using Collection Initializer syntax with a list (code example)
List ListOfInts = new List {1,2,3};
Define : IEnumerable
Allows a collection to give others the ability to look at a list one item at a time.
Define a Dictionary (code example)
Dictionary phonebook = new Dictionary();
Define the problem generics address
Type-safety without creating many similar types
Two methods to remove items from a list
RemoveAt and Clear
Define a generic class (code example)
public class PracticeList { stuff }
What variable structure are generic type names commonly?
Using a single capital letter, generally ‘T’ for singles, or if multiple, like ‘TKey & TValue’
What is the default operator?
Used to get default value of a type, for example default(int)
Default values for integer, float, bool, char, enum, strings & arrays, struct
integer & float : 0 bool : false char : 0 / null control character enum : 0 strings & arrays : null struct : each element takes default value for its type
Generic classes can only have one generic type parameter
false
Generic type constraints limit what can be used for the generic type
true
Constraints let you use the methods of the thing you are constraining to
True