O'Reilly logo

Learning C# 3.0 by Brian MacDonald, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7: Classes and Objects

Quiz Solutions

Solution to Question 7-1. A class defines a new type; an object is a single instance of that type.

Solution to Question 7-2. The keyword private indicates that access is limited to methods of the defining class.

Solution to Question 7-3. The keyword public indicates that access is available to methods in any class.

Solution to Question 7-4. When you create an instance of an object, the class’s constructor is called.

Solution to Question 7-5. A default constructor is a constructor that takes no parameters. If you do not create any constructor at all for your class, a default constructor is implicitly created.

Solution to Question 7-6. None. A constructor is not defined to return a type, and is not marked void.

Solution to Question 7-7. You can initialize the value of a member variable either in the constructor, using assignment, or when the member variable is created:

private int myVariable = 88;

Technically, only the latter is truly initialization; assigning it in the constructor is not as efficient.

Solution to Question 7-8. this refers to the object itself—the current instance of the class.

Solution to Question 7-9. A static method has no this reference. It does not belong to an instance; it belongs to the class and can call only other static methods.

You access a static method through the name of the class:

Dog myDog = new Dog(  );
myDog.InstanceMethod(  );
Dog.StaticMethod(  );

Of course, from within any method (including static methods), you can ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required