Answers to Chapter 21 Review Questions

   


Chapter 21

1:

How can you make the code between #if and #endif of the following lines be included in the compilation

 #if TRIALEDITION     <Some code> #endif 
A:

By letting the following line be the first line in the source code:

 #define TRIALEDITION 
2:

Is the second line in the following two lines of code valid? Why or why not?

 using System; #define 
A:

No, it is invalid. #define and #undef must precede any non-preprocessor directives in the source code.

3:

Add the necessary preprocessor directives and identifiers to the following lines of code, so that <Code part 1> is excluded from the compilation and <Code part 2> is included.

 ... #if     <Code part 1> #endif ... #if     <Code part 2> #endif ... 
A:
 #undef PART1 #define PART2 #if PART1     <Code part 1> #endif ... #if PART2     <Code part 2> #endif 
4:

What is the major difference between standard comments and program documentation?

A:

Standard comments are read in conjunction with the source code. Program documentation is read separately and independently from the source code. Standard comments start with // or are surrounded by /* */, whereas documentation comments begin with /// and contain XML tags.

5:

What does a documentation comment consist of? Give an example.

A:

A documentation comment starts with /// and consists of XML tags and descriptive text. For example

 ///<summary>The Rocket class represents a rocket in a rocket simulation </summary> 
6:

Which command must you give to the compiler to let it transform the documentation comments into a separate XML document?

A:

The following compiler command

 csc /doc: <XML_FileName>.xml <FileName>.cs 

generates an .xml documentation file called <XML_FileName>.xml based on the source code file <FileName>.cs.

7:

What is the main purpose of attributes?

A:

Attributes let you add declarative information to the elements in your source code beyond what is possible with the already existing C# keywords and language constructs.

8:

Can any attribute be used to decorate any code element?

A:

No, many attributes are meant to decorate only one or a few types of source code elements, such as just methods or just classes.

9:

Name a couple of .NET's predefined attributes and briefly describe what they are used for.

A:

System.Diagnostics.ConditionalAttribute: Prevent methods from being compiled and called.

System.ObsoleteAttribute: Mark selected elements obsolete. Will produce warnings or errors if these elements are used by other parts of the code.

10:

As mentioned earlier, .NET contains an attribute called XmlArrayAttribute; it exists in the System.Xml.Serialization namespace. Which of the following annotations are correct (there can be more than one). (Assume the line using System.Xml. Serialization; is included at the beginning of the source):

  1. [System.Xml.Serialization.XmlArrayAttribute]

  2. [XmlArrayAttribute]

  3. [XmlArray]

  4. [Xml]

  5. [Array]

A:

a, b, and c.

11:

What is an attribute's positional parameter? Give an example.

A:

Some attributes accept arguments that must be positioned in a certain order to be assigned to the correct attribute parameters. These arguments are referred to as positional parameters.

Example: "Use the Calculate method instead" and true are both positional parameters in the following attribute annotation:

 [System.ObsoleteAttribute("Use the Calculate method instead", true)] 
12:

Suppose your source code contains a method with the following header:

 public void StartOperation() {     ... } 

Which attribute can you use to prevent any methods from calling this method? Provide the necessary annotation.

A:

If TEST is undefined, the following annotation will prevent the StartOperation method from being compiled and called:

 [ConditionalAttribute("TEST")] public void StartOperation() {     ... } 
13:

Can you use the same procedure as in question 12 to prevent any calls to the following method? Why or why not?

 public int Sum(int x, int y) {     ... } 
A:

No, the ConditionalAttribute can only be applied on methods with the return type void.

14:

What is a named parameter?

A:

As opposed to positional parameters, named parameters can be positioned in any order you might choose. However, the named parameter is assigned a value by including its name followed by an equals sign (=) followed by the value, as shown in the following:

 <Named_parameter_name> = <Value> 

Answers to Chapter 21 Programming Exercises

1:

Assume that you are writing the source code from which three different program versions will be compiled a Standard Version, a Professional Version, and an Enterprise Version. In this case, the program simply contains three different WriteLine statements that write the following three lines:

A:  "This is the standard version" B:  "This is the professional version" C:   "This is the enterprise version" 

The Standard Version only prints line A, the Professional Version only prints line B, and the Enterprise Version only prints line C. Use preprocessor directives so that you can easily control (without deleting and adding WriteLine statements, but instead by defining and undefining identifiers) which of the three WriteLine statements is being included in the program.

A:

Exercise 1: The following represents the professional version. By simply changing which of the three identifiers STANDARDVERSION, PROFESSIONALVERSION, and ENTERPRISEVERSION is defined, you can easily control which version is compiled.

 #undef STANDARDVERSION #define PROFESSIONALVERSION #undef ENTERPRISEVERSION using System; class BliposExplorer {     public static void Main()     {       #if STANDARDVERSION         Console.WriteLine("This is the standard version");       #endif       #if PROFESSIONALVERSION         Console.WriteLine("This is the professional version");       #endif       #if ENTERPRISEVERSION         Console.WriteLine("This is the enterprise version");       #endif     } } 
2:

Use document comments and the <summary> and <remarks> tags to document the following program. Make sure you provide descriptions for all classes and methods. Use the compiler to extract the corresponding XML document.

 using System; class Calculator {         public static int Sum(int x, int y)         {                 return x + y;         }         public static int Product(int x, int y)         {                 return x * y;         } } class RocketScientist {             public static void Main()             { Console.WriteLine("Estimated time to go to Mars: { 0} ",          graphics/ccc.gifEstimatedTimeToMars(23));             }             public static uint EstimatedTimeToMars(int x)             {                     return (uint) (Calculator.Sum(x, 24) + Calculator.Product(x, 21));             } } 
A:

Exercise 2: To extract the documentation, remember to use the /doc: compiler switch.

 using System; ///<summary> Calculator contains methods for solving arithmetic calculations </summary> ///<remarks> Contains the Sum and Product methods </remarks> class Calculator {      ///<summary> Sum calculates the sum of two ints </summary>     public static int Sum(int x, int y)     {         return x + y;     }      ///<summary> Product calculates the product of two ints </summary>     public static int Product(int x, int y)     {         return x * y;     } } ///<summary> RocketScientist can perform sophisticated calculations related to rockets </ graphics/ccc.gifsummary> class RocketScientist {     public static void Main()     {         Console.WriteLine("Estimated time to go to Mars: {0}", EstimatedTimeToMars(23));     }      ///<summary> Estimates the time it will take to go to Mars </summary>     public static uint EstimatedTimeToMars(int x)     {         return (uint) (Calculator.Sum(x, 24) + Calculator.Product(x, 21));     } } 
3:

Consider the source code in Listing 21.4.

Listing 21.4 Train.cs
using System; class Train {     private uint distance = 0;     public void MoveBySteam(uint addDistance)     {         Console.WriteLine("Moving { 0}  kilometers by steam", addDistance);         distance += addDistance;     }     public void MoveByElectricity(uint addDistance)     {         Console.WriteLine("Moving { 0}  kilometer by electricy", addDistance);         distance += addDistance;     }     public void TestingDistance()     {         Console.WriteLine("Testing. distance instance variable: { 0} ", distance);     } } class Tester {     public static void Main()     {         Train orientExpress = new Train();         orientExpress.MoveBySteam(100);         orientExpress.MoveByElectricity(200);         orientExpress.TestingDistance();     } } 
  1. The Train class's MoveBySteam method in Listing 21.4 has become obsolete and you want the compiler to report a warning whenever it encounters a call to this method during compilation. The warning must announce that the programmer should instead use the MoveByElectricity method.

  2. The TestingDistance method should only be called during the testing of the program. Use an attribute to enable you to switch on and off any calls to this method in an easy fashion.

  3. You want to test what happens to the program if no calls are made to MoveBySteam method, while still maintaining the warning introduced in exercise a. You want to easily be able to switch this test on and of and do this independently of the calls to TestingDistance. In other words, you want the possibility of switching off the calls to MoveBySteam while the TestingDistance method is still being called, and vice versa.

  4. Because of the information you gain from the experiment you make in exercise c, you now want to make sure that no one is calling the MoveBySteam method. Make the compiler report an error instead of just a warning when it encounters a method call to this method in the source code.

A:

Exercise 3:

  1. Insert the following line just before the MoveBySteam method header:

     [Obsolete("Use MoveByElectricity instead")] 
  2. Include the following line just before the TestingDistance method header:

     [System.Diagnostics.Conditional("TEST")] 

    If you want TestingDistance to be called, include the line

     #define TEST 

    at the start of the source code; otherwise, don't write anything or include the following line:

     #undef TEST 
  3. The following shows what the code might look like after answering points a, b, and c. Defining TEST in the first line allows TestingDistance to be called. Undefining STEAM in the second line prevents MoveBySteam from being called.

     #define TEST #undef STEAM using System; class Train {     private uint distance = 0;       [System.Diagnostics.Conditional("STEAM")]       [Obsolete("Use MoveByElectricity instead")]     public void MoveBySteam(uint addDistance)     {   Console.WriteLine("Moving {0} kilometers by steam", addDistance);   distance += addDistance;   }                    public void MoveByElectricity(uint addDistance)       {           Console.WriteLine("Moving {0} kilometer by electricy", addDistance);           distance += addDistance;       }       [System.Diagnostics.Conditional("TEST")]       public void TestingDistance()       {           Console.WriteLine("Testing. distance instance variable: {0}", distance);       }   }   class Tester   {       public static void Main()       {           Train orientExpress = new Train();           orientExpreas.MoveBySteam(100);           orientExpress.MoveByElectricity(200);           orientExpress.TestingDistance();       }   } 
  4. You need to adjust the Obsolete attribute annotation of MoveBySteam to the following, which now includes the Boolean value true.

 [Obsolete("Use MoveByElectricity instead", true)] 


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net