What We Do

  • Our Focus is You
    We focus on your online needs so that you can focus on your customer's needs. There are numerous way in which we can help you. We begin by reviewing your business goals, your marketing objectives, and your competitive position, and then we develop your online presence to meet your needs.
  • Dynamic Content
    Today's web applications require dynamically driven content, and much of that content is provided by a database and a programming language operating in harmony to produce the desired results. MySQL and PHP are two of the most popular web development tools utilized to produce dynamic web sites. We can help you take control and begin building truly dynamic web content which is easier to maintain, is more responsive to your users, and can alter its appearance based upon differing situations. Contact Us today to discuss your custom development needs.
  • Content Management Systems (CMS)
    We can deploy CMS applications to allow organizations to edit content easily, which significantly cuts down on costly professional maintenance fees. Our CMS expertise covers Joomla, Wordpress and Blogger platforms. We are ready to help you choose which works best for your specific needs. Contact Us today to begin taking control of your content.
Powered by Blogger.

C# Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

We have already discussed various data types. The basic value types provided in C# can be categorized as:
TypeExample
Integral typessbyte, byte, short, ushort, int, uint, long, ulong and char
Floating point typesfloat and double
Decimal typesdecimal
Boolean typestrue or false values, as assigned
Nullable typesNullable data types
C# also allows defining other value types of variable like enum and reference types of variables like class, which we will cover in subsequent chapters. For this chapter, let us study only basic variable types.

Variable Definition in C#

Syntax for variable definition in C# is:

<data_type> <variable_list>;
Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, etc., and variable_list may consist of one or more identifier names separated by commas.

Some valid variable definitions are shown here:

int i, j, k;
char c, ch;
float f, salary;
double d;
You can initialize a variable at the time of definition as:

int i = 100;

Variable Initialization in C#

Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is:

variable_name = value;
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as:

<data_type> <variable_name> = value;
Some examples are:
int d = 3, f = 5;    /* initializing d and f. */
byte z = 22;         /* initializes z. */
double pi = 3.14159; /* declares an approximation of pi. */
char x = 'x';        /* the variable x has the value 'x'. */
It is a good programming practice to initialize variables properly, otherwise sometimes program would produce unexpected result.

Try the following example which makes use of various types of variables:

namespace VariableDefinition
{
    class Program
    {
        static void Main(string[] args)
        {
            short a;
            int b ;
            double c;

            /* actual initialization */
            a = 10;
            b = 20;
            c = a + b;
            Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
            Console.ReadLine();
        }
    }
}
When the above code is compiled and executed, it produces the following result:

a = 10, b = 20, c = 30

Accepting Values from User

The Console class in the System namespace provides a function ReadLine() for accepting input from the user and store it into a variable.
For example,

int num;
num = Convert.ToInt32(Console.ReadLine());

The function Convert.ToInt32() converts the data entered by the user to int data type, because Console.ReadLine() accepts the data in string format.

Lvalues and Rvalues in C#:

There are two kinds of expressions in C#:

lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.

rvalue: An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side. Following is a valid statement:

int g = 20;
But following is not a valid statement and would generate compile-time error:

10 = 20;