C# Tutorial - Application 3 - Adding Two Numbers


Create a new console Application called App03. We are going to create a simple application to add two whole numbers together and output the result. The full listing for the application is given below.

using System;
using System.Collections.Generic;
using System.Text;

namespace App03
{
  class Program
  {
    static void Main(string[] args)
    {
      // Storage
      int value1 = 0;
      int value2 = 0;
      int total = 0;

      // Get first number
      Console.Write("Enter a whole number: ");
      value1 = System.Convert.ToInt32(Console.ReadLine());

      // Get second number
      Console.Write("Enter another whole number: ");
      value2 = System.Convert.ToInt32(Console.ReadLine());

      // Add them together
      total = value1 + value2;

      // Output the answer
      Console.WriteLine("The total is {0}", total);
    }
  }
}

This application introduces integers (whole numbers). It asks for two whole numbers and converts the strings entered into integer values. These are then added together and the output is displayed on the screen. The conversion is done using the Convert class that is found in the System namespace. The method ToInt32 will convert the object passed to it into a 32 bit integer value if it can. If you enter a value that is not a whole number, then the application will crash. Later on we will change the program so that it does not crash.

You can follow what is happening in the application by reading the comments that appear above the code that they are describing. Comments make it easier for other developers to understand what your code is doing as long as they are descriptive and do not just copy the code into pseudo language.

In the output line, the number inside a set of curly braces is known as a format. This indicates that the variable found after the string (total) will be formatted to appear in the string instead of the format.

Enter a whole number: 12
Enter another whole number: 13
The total is 25

System.Convert Class

This class can convert one base data type into another base data type. What this means is that the class returns a type whose value is equivalent to the value of a specified type. The supported base types are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime and String.

When using the Convert class, an exception will be thrown if the conversion cannot be performed, more on exceptions later.

A conversion which cannot create a meaningful result will throw an InvalidCastException. An exception is thrown for conversions from Char to Boolean, Single, Double, Decimal or DateTime, and from those types to Char. An exception is thrown for conversions from DateTime to any type except String, and from any type, except String, to DateTime.

An exception will not be thrown if the conversion of a numeric type results in a loss of precision (that is, the loss of some least significant digits). However, an exception will be thrown if the result is larger than can be represented by the particular conversion method's return value type.

In our example application, if you enter characters, then the conversion to a numeric value cannot be performed and an exception is thrown and the application crashes. In the next application we will stop the application from crashing.


<< Previous Contents Next >>

© Publicjoe, 2008