Chapter 3: C# Language Fundamentals

Quiz

Solution to Question 3–1.

True or False.

Solution to Question 3–2.

int is the C# alias for the .NET Int32 type. They can be used interchangeably.

Solution to Question 3–3.

All except the last, which requires an explicit cast:

int newInt = (int) myLong;
Solution to Question 3–4.

A uint is an unsigned int and can hold only positive numbers, but it can hold a positive value twice as big as an int (4 million+ rather than 2 million+).

Solution to Question 3–5.

A float takes four bytes and a double takes eight bytes, and thus a double can represent much larger values with greater precision.

Exercise 2-3

Figure A-3. Exercise 2-3

Solution to Question 3–6.

In C, if you wish to use a variable of any type (pass it as a parameter to a method), you must assign it a value.

Solution to Question 3–7.

You refer to the constant like this:

Temperatures.LightJacketWeather

Its value is 33.

Solution to Question 3–8.

Any statement that evaluates to a value.

Exercises

Solution to Exercise 3-1.

Write a short program creating and initializing each of the following types of variables: int, float, double, char, and then outputting the values to the console.

namespace fundamentals
{
   class exercise
   {
      static void Main(  )
      {
         int myInt = 3;
         float myFloat = 4.25f;
         double myDouble = 123456789.9867;
         char myChar = 'A';
         System.Console.WriteLine("myInt: {0}, myFloat: {1}, myDouble: {2},
            myChar: {3}.", myInt, myFloat, myDouble, myChar);
      }
   }
}

The output should look like this:

myInt: 3, myFloat: 4.25, myDouble: 123456789.9876, myChar: A.
Solution to Exercise 3-2.

Modify the program in Exercise 3-1 to change the values of variables and output the values to the console a second time:

namespace fundamentals
{
   class exercise
   {
      static void Main(  )
      {
         // round one

         int myInt = 3;
         float myFloat = 4.25f;
         double myDouble = 123456789.9867;
         char myChar = 'A';
         System.Console.WriteLine("Round 1: myInt: {0}, myFloat: {1},
            myDouble: {2}, myChar: {3}.", myInt, myFloat, myDouble,
            myChar);

         // round two

         myInt = 5;
         float myFloat = 25.267f;
         myDouble = 987654321.1234;
         myChar = 'Z';
         System.Console.WriteLine("Round 2: myInt: {0}, myFloat: {1},
            myDouble: {2}, myChar: {3}.", myInt, myFloat, myDouble,
            myChar);
      }
   }
}

The output should look like this:

Round 1: myInt: 3, myFloat: 4.25, myDouble: 123456789.9876, myChar: A.
Round 2: myInt: 5, myFloat: 25.267, myDouble: 987654321.1234, myChar: Z.
Solution to Exercise 3-3.

Modify the program in Exercise 3-2 to declare a constant float Pi equal to 3.14159. Then assign a new value to pi (3.1) and output its value with the other variables statement. What happens when you try to compile this program?

namespace fundamentals
{
   class exercise
   {
      static void Main()
      {
         const float pi = 3.14159f;

         int myInt = 3;
         float myFloat = 4.25f;
         double myDouble = 123456789.9867;
         char myChar = 'A';
         pi = 3.1f;
         System.Console.WriteLine("Round 1: myInt: {0}, myFloat: {1},
            myDouble: {2}, myChar: {3}.  Pi: {4}", myInt, myFloat,
            myDouble, myChar, pi);

      }
   }
}

This program won’t compile, because you’re trying to assign a value to a constant. Instead, you receive a compiler error that reads, “The left-hand side of an assignment must be a variable, property or indexer.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset