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):
|
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> |
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} ", EstimatedTimeToMars(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 </ summary> 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.csusing 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(); } }
|
A: | Exercise 3:
[Obsolete("Use MoveByElectricity instead", true)] |