Programming Example 3: Using Primitive Data Types and Strings


This program illustrates the proper syntax for declaring a variable, initializing a variable, and using the assignment operator. It also illustrates the proper use of the String class and the Console class.

As you read through this example, keep the following rules in mind:

  1. Identifiers: In C# identifiers must begin with a letter or the underscore character. Identifiers may contain letters, digits, and the underscore character. All other characters are illegal.
  2. Declarations: Before a name can be used anywhere in a C# program, that name must be declared. That is, you must tell the compiler what kind of data that name is to be associated with. The compiler allocates space in the computers memory based on the type of data being stored.
  3. Initialization: When you declare a variable, you should give that variable some initial value. The C# language initializes values to zero, but to make sure that you don't get an unwanted surprise, initialize variables when you declare them.
  4. Assignment: You can cause a value to be stored in a variable by using the assignment operator "=".
  5. String class: Whenever we deal with strings of characters: names, words, phrases, etc., we use objects of the String class.
  6. The Console class represents the computer console. When a console application starts up, the standard input stream object and the standard output stream object are created automatically for you. The ReadLine method of the Console class will read characters typed at the keyboard. The WriteLine method will write data to the computer display.

This example program is located here.  An executable of this program can be found here.