Fundamentals of Variables

Introduction to Values

A value is a piece of information (called data) you need to use in your program. One way to use a value is to display it to the user. To do this, you can include the value in the parentheses of System.Console.Write(). Here are two examples:

class Exercise
{
    static void Main()
    {
        System.Console.Write(248);
    }
}

Practical LearningPractical Learning: Introducing Variables

  1. Start Microsoft Visual Studio
  2. To create a new application (if Microsoft Visual Studio was already opened, on the main menu, click File -> New -> Project...), on the Visual Studio 2019 dialog box, click Create a New Project)
  3. In the Create a New Project dialog box, click Language and click C#
  4. In the Filtered For: C# list, click Empty Project (.NET Framework) and click Next
  5. Change the Name to GeorgetownDryCleaningServices1
  6. Click Create
  7. To create a file for the code, on the main menu, click Project -> Add New Item...
  8. In the left list of the Add New Item dialog box, click Code
  9. In the middle list, click Code File
  10. Change the Name to OrdeProcessing
  11. Click Add
  12. Based on our introductions in the previous lesson, in the empty document, type the following:
    class Exercise
    {
        static void Main()
        {
        }
    }
  13. To execute the application to see the result, on the main menu, click Debug -> Start Without Debugging. This would produce:
    Press any key to continue . . .
  14. Press Enter and return to your programming environment

Intropduction to Variables

A variable is a value that is stored in the computer memory (the random access memory, also called RAM). Such a value can be retrieved from the computer memory and displayed to the user.

Declaring a Variable

Before using a variable in your application, you must let the compiler know. Letting the compiler know is referred to as declaring a variable.

To declare a variable, you must provide at least two pieces of information. Actually you have various options. We will start with one of them. One option to declare a variable is to provide the type of value, also called a data type, followed by a name for the variable. The formula to follow is:

data-type variable-name;

Initializing a Variable

When declaring a variable, you can give it a primary value. This is referred to as initializing the variable. You can use the following formula:

data-type variable-name = value;

This would be done as follows:

class Exercise
{
    static void Main()
    {
		data-type variable-name = desired-value;
    }
}

The Name of a Variable

A variable must have a name. There are rules you must follow to specify the name of a variable. To start, you must avoid reserved words, also called keywords. These keywords are:

abstract as async await base bool break
byte case catch char checked class const
continue decimal default delegate do double dynamic
enum else event extern false finally fixed
float for foreach goto if implicit in (foreach)
in (generic) int interface internal is lock long
namespace new (generic) new (inheritance) new (LINQ) new (variable) null object
operator out (generic) out (methods) override params private protected
public readonly ref return sbyte sealed short
sizeof stackalloc string struct switch static this (class)
this (indexer) throw true unsafe using uint ulong
ushort try typeof virtual void where while
      explicit   yield  

There are other names that are not considered C# keywords but should be avoided because they may cause a conflict in your code. They are referred to as contextual keywords. They are:

add descending global let remove var
alias group orderby select where (generic)  
ascending from into partial (method) set where (query)
async get join partial (type) value  
await          

There are some rules we will follow to name variables:

Besides these rules, you can also create your own rules but that follow the above restrictions. For example:

C# is case-sensitive. This means that the names Case, case, and CASE are completely different.

Practical LearningPractical Learning: Introducing a Variable

Introduction to Data Types: Strings

Introduction to Strings

A string one or a group of symbols, readable or not. The symbols can be letters (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, and Z), digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) are non-readable characters (` ~ ! @ # $ % ^ & * ( ) - _ = + [ { ] } \ | ; : ' < ? . / , > "). To create a string, include its symbol, symbols, or combination inside double-quotes.

Displaying a String

To display a string to the user, include its value in the parentheses of System.Console.Write(). Here is an example:

class Exercise
{
    static void Main()
    {
		System.Console.Write("Welcome to the World of C# Programming!");
    }
}

This would produce:

Welcome to the World of C# Programming!Press any key to continue . . .

Creating a New Line

As mentionned briefly in the previous lesson. if you use System.Console.Write(), everything displays on the same line. As an alternative, you can use System.Console.WriteLine(). In this case, after the string is displayed, whatever item comes next would display in the next line. Just as you can include a value in the parentheses of System.Console.Write(), you can put a value in the parentheses of System.Console.WriteLine(). Consider the following example:

class Exercise
{
    static void Main()
    {
		System.Console.WriteLine("Welcome to the World of C# Programming!");
    }
}

This would produce:

Welcome to the World of C# Programming!
Press any key to continue . . .

In the same way, you can use any combination of System.Console.Write() or of System.Console.WriteLine() in your code. Here are examples:

class CountryStatistics
{
    static void Main()
    {
        System.Console.Write("Country Name:");
        System.Console.Write(" ");
        System.Console.WriteLine("Australia");
    }
}

This would produce:

Country Name: Australia
Press any key to continue . . .

Remember that you can leave the parentheses of System.Console.WriteLine() empty or include something in it but you can never leave the parentheses of System.Console.Write() empty.

Practical LearningPractical Learning: Displaying a String

  1. Change the document as follows:
    class Exercise
    {
        static void Main()
        {
            // data-type variable - name = value;
    
            System.Console.WriteLine("-/-Georgetown Cleaning Services -/-");
            System.Console.WriteLine("===================================");
        }
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging. This would produce:
    -/-Georgetown Cleaning Services -/-
    ===================================
    Press any key to continue . . .
  3. Press Enter and return to your programming environment

A String Variable

You may want to store the value of a string in a variable. To let you do this, the C# language provides a data type named string. Use it to declare a string-based variable. To provide the value of the variable, specify its value in double-quotes. To display the value, put the name of the variable in the parentheses of System.Console.Write() or of System.Console.WriteLine().

Practical LearningPractical Learning: Introducing String Variables

  1. Change the document as follows:
    class Exercise
    {
        static void Main()
        {
            string customerName = "James Burreck";
            string homePhone = "(202) 301-7030";
    
            System.Console.WriteLine("-/-Georgetown Cleaning Services -/-");
            System.Console.WriteLine("===================================");
    
            System.Console.Write("Customer:   ");
            System.Console.WriteLine(customerName);
            System.Console.Write("Home Phone: ");
            System.Console.WriteLine(homePhone);
            System.Console.WriteLine("===================================");
        }
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging. This would produce:
    -/-Georgetown Cleaning Services -/-
    ===================================
    Customer:   James Burreck
    Home Phone: (202) 301-7030
    ===================================
    Press any key to continue . . .
  3. Press Enter and return to your programming environment

Introduction to Natural Numbers

Overview

A natural number is a value that contains only digits. To recognize such values, the C# language provides a data type named int. Use it to declare a variable that would hold a natural number. Here is an example:

class Exercise
{
    static void Main()
    {
        int age;
    }
}

The Value of an Integral Variable

To specify the variable of an int type, provide a value that contains only digits. Here is an example:

class Order
{
    static void Main()
    {
        int age = 15;
    }
}

After specifying the value of the variable, to display that value to the user, you can include the name of the variable in the parentheses of System.Console.Write() or System.Console.WriteLine(). Here are two examples:

class Order
{
    static void Main()
    {
        int monthlySalary = 3288;
        
        System.Console.Write("Monthly Salary: ");
        System.Console.WriteLine(monthlySalary);
    }
}

This would produce:

Monthly Salary: 3288
Press any key to continue . . .

The value of an integer can be between -2147483648 and 2147484647 (or -2,147,483,648 and 2,147,484,647). If you need to use a large value, to make it easier to humanly read, you can separate the thousands by underscores. Here are examples:

class CountriesStatistics
{
    static void Main()
    {
        int areaChina = 9_596_961;
        int areaCanada = 9_984_670;
        int areaBurkinaFaso = 275_200;
        int areaDjibouti = 23_200;

        System.Console.WriteLine("Countries Areas");
        System.Console.WriteLine("---------------------");
        System.Console.Write("Djibouti: ");
        System.Console.WriteLine(areaDjibouti);
        System.Console.Write("Burkina Faso: ");
        System.Console.WriteLine(areaBurkinaFaso);
        System.Console.Write("China: ");
        System.Console.WriteLine(areaChina);
        System.Console.Write("Canada: ");
        System.Console.WriteLine(areaCanada);
    }
}

This would produce:

Countries Areas
---------------------
Djibouti: 23200
Burkina Faso: 275200
China: 9596961
Canada: 9984670
Press any key to continue . . .

Practical LearningPractical Learning: Introducing Integers

  1. Change the document as follows:
    class Exercise
    {
        static void Main()
        {
            string customerName = "James Burreck";
            string homePhone = "(202) 301-7030";
            int numberOfShirts = 1;
            int numberOfPants = 1;
            int numberOfDresses = 1;
            int orderMonth = 3;
            int orderDay = 15;
            int orderYear = 2020;
    
            System.Console.WriteLine("-/-Georgetown Cleaning Services -/-");
            System.Console.WriteLine("===================================");
    
            System.Console.Write("Customer:   ");
            System.Console.WriteLine(customerName);
            System.Console.Write("Home Phone: ");
            System.Console.WriteLine(homePhone);
            System.Console.Write("Order Date: ");
            System.Console.Write(orderMonth);
            System.Console.Write('/');
            System.Console.Write(orderDay);
            System.Console.Write('/');
            System.Console.WriteLine(orderYear);
            System.Console.WriteLine("-----------------------------------");
            System.Console.WriteLine("Item Type  Qty");
            System.Console.WriteLine("-----------------------------------");
            System.Console.Write("Shirts      ");
            System.Console.WriteLine(numberOfShirts);
            System.Console.Write("Pants       ");
            System.Console.WriteLine(numberOfPants);
            System.Console.Write("Dresses     ");
            System.Console.WriteLine(numberOfDresses);
            System.Console.WriteLine("===================================");
        }
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging. This would produce:
    -/-Georgetown Cleaning Services -/-
    ===================================
    Customer:   James Burreck
    Home Phone: (202) 301-7030
    Order Date: 3/15/2020
    -----------------------------------
    Item Type  Qty
    -----------------------------------
    Shirts      1
    Pants       1
    Dresses     1
    ===================================
    Press any key to continue . . .
  3. Press Enter and return to your programming environment

Introduction to Floating-Point Numbers

Introduction to Decimal Numbers

A floating-point number is a number made of either only digits or two digit parts separated by a symbol referred to as a decimal separator. As one way to support floating-point numbers, the C# language provides a data type named double. Use it to declare a variable for a number. To provide a value for the value, you can use the same types of numbers we saw for integers. Here are examples:

class StatesStatistics
{
    static void Main()
    {
        double areaMaine = 35385;
        double areaAlaska = 1_723_337;

        System.Console.WriteLine("States Areas");
        System.Console.Write("Maine: ");
        System.Console.WriteLine(areaMaine);
        System.Console.Write("Alaska: ");
        System.Console.WriteLine(areaAlaska);

    }
}

This would produce:

Monthly Salary: 3288
Press any key to continue . . .

A Floating-Point Number with Precision

The primary difference between an integer and a floating-point number is that a decimal number can include a second part referred to as a precision. To specify the precision of a number, after the natural part, add the symbol used as the decimal separator. In US English, this is the period. Here is an example:

class Employment
{
    static void Main()
    {
        double hourlySalary = 25.85;

        System.Console.Write("Hourly Salary: ");
        System.Console.WriteLine(hourlySalary);
    }
}

This would produce:

Hourly Salary: 25.85
Press any key to continue . . .

If the integral part is large, you can use it "as is" or you can separate its thousansds with underscores. Here are examples:

class StatesStatistics
{
    static void Main()
    {
        double areaGuam = 570.62;
        double areaAlaska = 665_384.04;
        double areaSouthDakota = 77115.68;
        double areaTennessee = 42_144.25;

        System.Console.WriteLine("States Areas");
        System.Console.WriteLine("=======================");
        System.Console.Write("Guam: ");
        System.Console.WriteLine(areaGuam);
        System.Console.Write("Alaska: ");
        System.Console.WriteLine(areaAlaska);
        System.Console.Write("Tennessee: ");
        System.Console.WriteLine(areaTennessee);
        System.Console.Write("South Dakota: ");
        System.Console.WriteLine(areaSouthDakota);
    }
}

This would produce:

States Areas
=======================
Guam: 570.62
Alaska: 665384.04
Tennessee: 42144.25
South Dakota: 77115.68
Press any key to continue . . .

Practical LearningPractical Learning: Introducing Double-Precision Numbers

  1. Change the document as follows:
    class Exercise
    {
        static void Main()
        {
            string customerName = "James Burreck";
            string homePhone = "(202) 301-7030";
            int numberOfShirts = 1;
            int numberOfPants = 1;
            int numberOfDresses = 1;
            double priceOneShirt = 0.95;
            double priceAPairOfPants = 2.95;
            double priceOneDress = 4.55;
            int orderMonth = 3;
            int orderDay = 15;
            int orderYear = 2020;
    
            System.Console.WriteLine("-/-Georgetown Cleaning Services -/-");
            System.Console.WriteLine("===================================");
    
            System.Console.Write("Customer:   ");
            System.Console.WriteLine(customerName);
            System.Console.Write("Home Phone: ");
            System.Console.WriteLine(homePhone);
            System.Console.Write("Order Date: ");
            System.Console.Write(orderMonth);
            System.Console.Write('/');
            System.Console.Write(orderDay);
            System.Console.Write('/');
            System.Console.WriteLine(orderYear);
            System.Console.WriteLine("-----------------------------------");
            System.Console.WriteLine("Item Type  Qty Sub-Total");
            System.Console.WriteLine("------------------------");
            System.Console.Write("Shirts      ");
            System.Console.Write(numberOfShirts);
            System.Console.Write("     ");
            System.Console.WriteLine(priceOneShirt);
            System.Console.Write("Pants       ");
            System.Console.Write(numberOfPants);
            System.Console.Write("     ");
            System.Console.WriteLine(priceAPairOfPants);
            System.Console.Write("Dresses     ");
            System.Console.Write(numberOfDresses);
            System.Console.Write("     ");
            System.Console.WriteLine(priceOneDress);
            System.Console.WriteLine("===================================");
        }
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging. This would produce:
    -/-Georgetown Cleaning Services -/-
    ===================================
    Customer:   James Burreck
    Home Phone: (202) 301-7030
    Order Date: 3/15/2020
    -----------------------------------
    Item Type  Qty
    -----------------------------------
    Shirts      1
    Pants       1
    Dresses     1
    ===================================
    Press any key to continue . . .
  3. Press Enter and return to your programming environment

At a Variable

As mentioned previously, you must avoid using keywords to name your variables. If you have to violate that rule, that is, if you want to use a keyword as the name of a variable, start the name of the variable with the @ sign. Here are examples:

public class Program
{
    static void Main()
    {
        int @double = 3648;
        double @static = 248.59;
        string @class = "Nuclear Energy";
    }
}

After declaring a variable with an @name, you can use the variable but make sure you always use the @symbol on its name. Here are examples:

class Exercise
{
    static void Main()
    {
        int    @double = 3648;
        double @static = 248.59;
        string @class  = "Nuclear Energy";

        System.Console.WriteLine("Values");
        System.Console.WriteLine("-------------------------");
        System.Console.Write("Distance: ");
        System.Console.Write(@double);
        System.Console.WriteLine(" miles");
        System.Console.Write("Net Pay:  ");
        System.Console.WriteLine(@static);
        System.Console.Write("Industry: ");
        System.Console.WriteLine(@class);

        System.Console.WriteLine("==================================");
    }
}

This would produce:

Values
-------------------------
Distance: 3648 miles
Net Pay:  248.59
Industry: Nuclear Energy
==================================
Press any key to continue . . .

Even if you use variable names that don't use C# keywords, you can start the name of any variable with the @ symbol. Here are examples:

class SchoolUniform
{
    static void Main()
    {
        string @description = "Short Sleeve Shirt";
        int @quantity = 26;
        string @size = "Medium";
        double @unitPrice = 8.85;
    }
}

When accessing the variable, if you want, you can start the name of the variable with the @ symbol or you can omit the @ symbol. Here are examples:

class SchoolUniform
{
    static void Main()
    {
        string @description = "Short Sleeve Shirt";
        int    @quantity    = 26;
        string @size        = "Medium";
        double @unitPrice   = 8.85;

        System.Console.WriteLine("School Uniform");
        System.Console.WriteLine("------------------------------");
        System.Console.Write("Item Name:  ");
        System.Console.WriteLine(@description);
        System.Console.Write("Item Size:  ");
        System.Console.WriteLine(size);
        System.Console.Write("Quantity:   ");
        System.Console.WriteLine(@quantity);
        System.Console.Write("Unit Price: ");
        System.Console.WriteLine(unitPrice);
        System.Console.WriteLine("===============================");
    }
}

This would produce:

School Uniform
------------------------------
Item Name:  Short Sleeve Shirt
Item Size:  Medium
Quantity:   26
Unit Price: 8.85
===============================
Press any key to continue . . .

Even if you declare a variable whose name doesn't use a keyword, when accessing the variable, you can start its name with or without the @ symbol. If you decide to start a name with the @ symbol, you can use only one @ symbol on the name. Here are examples:

class StatesStatistics
{
    static void Main()
    {
        double areaGuam = 570.62;
        double areaAlaska = 665_384.04;
        double areaSouthDakota = 77115.68;
        double areaTennessee = 42_144.25;

        System.Console.WriteLine("States Areas");
        System.Console.WriteLine("=======================");
        System.Console.Write("Guam: ");
        System.Console.WriteLine(@areaGuam);
        System.Console.Write("Alaska: ");
        System.Console.WriteLine(areaAlaska);
        System.Console.Write("Tennessee: ");
        System.Console.WriteLine(@areaTennessee);
        System.Console.Write("South Dakota: ");
        System.Console.WriteLine(areaSouthDakota);
    }
}

This would produce:

States Areas
_________________________
Guam: 570.62
Alaska: 665384.04
Tennessee: 42144.25
South Dakota: 77115.68
============================
Press any key to continue . . .

Techniques of Declaring and Using Variables

Updating a Variable

When declaring a variable, you don't have to initialize it. This means that you can declare a variable, perform other operations, and then on another line, specify the value of the variable. The rule to observe is that, if you decide to display the value of the variable, you must first specify its value. This can be done as follow:

class Exercise
{
    static void Main()
    {
        data-type variable-name;

        // Optionally do other things here...
        
        variable-name = desired-value;
    }
}

Here are examples:

class Employment
{
    static void Main()
    {
        string fullName;
        double hourlySalary;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("=======================");

        hourlySalary = 22.27;
        fullName = "Martial Engolo";
        
        System.Console.Write("Employee Name: ");
        System.Console.WriteLine(fullName);
        System.Console.Write("Houly Rate: ");
        System.Console.WriteLine(hourlySalary);
    }
}

This would produce:

Employee Details
=======================
Employee Name: Martial Engolo
Houly Rate: 22.27
Press any key to continue . . .

Still, you can initialize a variable, perform other operations, and then specify a new value for the variable. This means that, at any time, you can change the value of a variable. This is also referred to as updating the variable. When you update a variable, its previous value is lost and it assumes the new value. Here are examples:

class EmployeesRecords
{
    static void Main()
    {
        string fullName = "Martial Engolo";
        double hourlySalary = 22.27;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("=======================");
        System.Console.Write("Employee Name: ");
        System.Console.WriteLine(fullName);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);

        hourlySalary = 35.08;
        fullName = "Annette Sandt";

        System.Console.WriteLine("----------------------");
        System.Console.Write("Employee Name: ");
        System.Console.WriteLine(fullName);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
    }
}

This would produce:

Employee Details
=======================
Employee Name: Martial Engolo
Hourly Rate: 22.27
----------------------
Employee Name: Annette Sandt
Hourly Rate: 35.08
Press any key to continue . . .

Declaring Many Variables

As we have seen so far, you can declare as many variables as you want. Here are examples:

class EmployeeDetails
{
    static void Main()
    {
        string status = "Full Time";
        string firstName = "Martial";
        double hourlySalary = 22.27;
        string lastName = "Engolo";
        double timeWorked = 42.50;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
    }
}

This would produce:

Employee Details
============================
Employee Name: Martial Engolo
Status: Full Time
Hourly Rate: 22.27
Time Worked: 42.5
Press any key to continue . . .

If you want to declare variables of the same type, you can use their common data type, followed by names separated by commas, and ending with a semicolon. Here is an example:

int width, height, depth;

You can initialize each variable with its own value. You don't have to initialize all variables, only those whose values you want to specify. This can be done as follows:

int width = 228, height, depth = 39;

Other than that, you can use each variable as we have done so far. Here are examples:

class Employment
{
    static void Main()
    {
        string firstName = "Martial", lastName = "Engolo";
        string status = "Full Time";
        double hourlySalary = 22.27, timeWorked = 42.50;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
    }
}

Practical LearningPractical Learning: Declaring Many Variables

  1. Change the document as follows:
    class Exercise
    {
        static void Main()
        {
            string customerName      = ""Geneviève Harrens",
                   homePhone         = "(410) 386-2747";
            int    numberOfShirts    = 1,
                   numberOfPants     = 1,
                   numberOfDresses   = 1;
            double priceOneShirt     = 1.35,
                   priceAPairOfPants = 2.25,
                   priceOneDress     = 3.50;
            int    orderMonth        = 6,
                   orderDay          = 22,
                   orderYear         = 2020;
    
            System.Console.WriteLine("-/-Georgetown Cleaning Services -/-");
            System.Console.WriteLine("===================================");
    
            System.Console.Write("Customer:   ");
            System.Console.WriteLine(customerName);
            System.Console.Write("Home Phone: ");
            System.Console.WriteLine(homePhone);
            System.Console.Write("Order Date: ");
            System.Console.Write(orderMonth);
            System.Console.Write('/');
            System.Console.Write(orderDay);
            System.Console.Write('/');
            System.Console.WriteLine(orderYear);
            System.Console.WriteLine("-----------------------------------");
            System.Console.WriteLine("Item Type  Qty Sub-Total");
            System.Console.WriteLine("------------------------");
            System.Console.Write("Shirts      ");
            System.Console.Write(numberOfShirts);
            System.Console.Write("     ");
            System.Console.WriteLine(priceOneShirt);
            System.Console.Write("Pants       ");
            System.Console.Write(numberOfPants);
            System.Console.Write("     ");
            System.Console.WriteLine(priceAPairOfPants);
            System.Console.Write("Dresses     ");
            System.Console.Write(numberOfDresses);
            System.Console.Write("     ");
            System.Console.WriteLine(priceOneDress);
            System.Console.WriteLine("===================================");
        }
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging. This would produce:
    -/-Georgetown Cleaning Services -/-
    ===================================
    Customer:   Geneviève Harrens
    Home Phone: (410) 386-2747
    Order Date: 6/22/2020
    -----------------------------------
    Item Type  Qty Sub-Total
    ------------------------
    Shirts      1     1.35
    Pants       1     2.25
    Dresses     1     3.50
    ===================================
    Press any key to continue . . .
  3. Press Enter and return to your programming environment

An Object Value

To further increase its flexibility, the C# language has a data type named object. That data type allows you to declare a variable of any type. When declaring the variable, assign a value of your choice to it. Here are examples:

class Employment
{
    static void Main()
    {
        object status = "Full Time";
        object firstName = "Martial";
        object lastName = "Engolo";
        object hourlySalary = 22.27;
        object timeWorked = 42.50;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
    }
}

You can use a common object keyword to declare many variables. Here are examples:

class Employment
{
    static void Main()
    {
        object status = "Full Time";
        object firstName = "Martial", lastName = "Engolo";
        object hourlySalary = 22.27, timeWorked = 42.50;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
    }
}

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2021, FunctionX Next