Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
17 Cards in this Set
- Front
- Back
- 3rd side (hint)
What are the three kinds of class constructors?
|
Instance
Private Static |
Ips
|
|
What is the function of an 'instance' constructor?
|
To create and initialize instances of a class.
|
|
|
What is the function of a private constructor?
|
Used to create a class that cannot be instantiated. Typically these are classes containing only static members. Use of a private constructor prevents C# from invoking the default constructor.
|
|
|
Can a class with a private constructor be inherited?
|
No.
|
|
|
Can a private class be used in a declaration?
|
Yes.
... privateConstructorClass foo; ... |
|
|
What might a class with a private constructor be used for?
|
A good use for a private constructor class is to encapsulate a bunch of related constants.
|
|
|
What is the purpose of a 'static' constructor?
|
A static constructor, like a class constructor, is usd to initialize an instance of a class. However, it is called by the system before the first instance of the class is created.
|
|
|
Show an example of a static constructor?
|
public class myClass
{ static myClass() { ... } } Note: since static constructors are not called directly they do not have access modifiers. |
|
|
What are the three major differences between a class constructor and a struct constructor?
|
1. Cannot contain parameterless constructors.
2. Members initialized to default values 3. Cannot use "base" initializers |
|
|
Will the following declaration compile:
public struct foo { int[] bar = new int[3]; } ... |
No. You will receive the following error message:
'Constructors.foo.bar': cannot have instance field initializers in structs |
|
|
What is the purpose of a destructor?
|
Destructors are used to destroy an instance of a class.
|
|
|
How is a destructor declared?
|
precede the class name with a tilde:
class foo { ~foo(){ <some code> } |
|
|
How is a destructor called?
|
A destructor cannot be called directly. It is invoked automatically by the garbage collector.
|
|
|
What method is called by the destructor even if you do not explicitly invoke it?
|
The base objects Finalize() method.
|
|
|
How do you force a garbage collect?
|
invoke the
GC.Collect method. This is not recommended because it may result in poorer performance than the .NET garbage collection algorthm. |
|
|
Why might you want to create a destructor?
|
1. To release resources being used by unmanaged code
2. To release a valuable/scarce external resource. |
|
|
What method must you use to cleanup external resource references?
|
You must implement a 'Dispose' method that is inherited for the 'IDisposable' interface.
|
|