Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

Workshop

Quiz

  1. What are the five access modifiers available in C#?

  2. What is the default accessibility for a class?

  3. What is a constructor?

  4. Can the default constructor of a class have parameters?

  5. Using the code shown in Listing 3.13, what is the output of the following statement?

    C c = new C(3, "C2");
    
  6. When can a read-only field be assigned?

  7. What is method overloading?

  8. Are there limitations when using automatic properties?

  9. What is a nested class?

  10. Can extension methods access private members of the type being extended?

  11. What happens when the new operator is executed?

Answers

  1. The five access modifiers available in C# are public, protected, internal, protected internal, and private.
  2. Classes default to internal accessibility but are allowed to have either public or internal declared accessibility. Nested classes default to private accessibility but are allowed to have any accessibility.
  3. A constructor is a special method that is executed automatically when you create an object to provide additional initialization actions.
  4. No, the default constructor of a class must always have no parameters.
  5. The output of the statement is

    Default Constructor
    C2
    3
    
  6. A read-only field can be initialized only as part of its declaration or in a constructor.
  7. Method overloading is creating more than one method of the same name that differs only by the number and type of parameters.
  8. Automatic properties do not provide a way to access the implicit backing field, do not enable you to specify additional statements that execute as part of the get or set accessor, and do not enable a mixture of regular and automatic syntax.
  9. A nested class is one that is fully enclosed inside another class declaration.
  10. Because extension methods are simply static methods, they do not have any special access to the type they extend. However, an extension method defined in the same assembly as the type being extended also has access to internal members of that type.
  11. The two primary actions that occur when the new operator is executed are 1) Memory is allocated from the heap and 2) the constructor for the class is executed to initialize the allocated memory.

Exercise

  1. Add a class to the PhotoViewer project that represents a photo. This class should be named Photo and be in the PhotoViewer namespace. The class should have the following private fields and a read-only property to retrieve the value of those fields:

    Data Type

    Field Name

    bool

    Exists

    BitmapFrame

    image

    Uri

    source

    Add the following constructor:

          public Photo(Uri path)
          {
             if (path.IsFile)
             {
                this.source = path;
             }
          }
    
  • + Share This
  • 🔖 Save To Your Account