1. C# Questions Flashcards
What’s the difference between reference and value types? Give examples for each.
Reference types are objects that store references to the actual data. A few examples are classes, interfaces, delegates, objects, and strings.
Value types actually hold values. Assigning one value type to another literally copies the value. A few examples are structs, enums, booleans, and numeric types.
What is an interface?
Interfaces are used to specify methods and properties that a derived class will have access to. It does not contain data or code in itself but is rather a contract that ensures that the classes that implement it will contain the specific methods and properties specified.
What’s an abstract class?
Abstract classes are similar to interfaces but they can contain code or data and a class can inherit from only one abstract class.
You can specify an abstract class’s methods and properties as virtual
to allow derived classes to override their implementation or abstract
to force them to.
What should you consider when deciding to use between an abstract class and an interface?
Whether you have logic that will be the same for all the derived classes. If you do, it’s best to use an abstract class. If not, an interface is a better choice.
What is a static class?
Static classes are classes that
- can’t be instantiated,
- are inherently sealed, and
- can only contain static members.
They are used to create data or functions that don’t need to be instantiated.
What are generics? Give an example.
Generics allow us to enforce type safety by letting us tailor methods, classes, structures and interfaces to a precise data type.
For example, we could have a single array class to store a list of users or products and when we use it, we access the items in the collection as a list of users or products. This way, we don’t have to use something like objects which at that point would force you to either do some boxing or unboxing.