1. C# Questions Flashcards

1
Q

What’s the difference between reference and value types? Give examples for each.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an interface?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s an abstract class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What should you consider when deciding to use between an abstract class and an interface?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a static class?

A

Static classes are classes that

  1. can’t be instantiated,
  2. are inherently sealed, and
  3. can only contain static members.

They are used to create data or functions that don’t need to be instantiated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are generics? Give an example.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly