Monday, 20 May 2013


We already have discussed classes.  Now we will go deep in programming, we will start discussing the basic programming part of C sharp.   We start with objects.
An object is an instance of a class.  You create objects to access member variables and member functions of a class.  Now let us discuss what are variables and functions.
Consider a situation where you have to create a program that accepts two numbers from a user and displays the sum of the numbers on the screen.  Now, while reading the numbers provide by the user, you need to store somewhere in the memory so that you can perform the add operation on the numbers.  You can store the numbers in the memory by using variables.  A variable is a location in the memory that has a name and contains a value.  The value could be integer, such as 27, a decimal, such as 9.85, or a character, such as ‘L’.  A variable is associated with a data type that defines the type tof data that can be stored in the variable.
For example, a variable called TennisPlayerName will ideally store characters, whereas a variable called High Score will store numbers.  A program refers to a variable by its name.
Naming Variables in C#
In C#, the following rules are used for naming variables:
·         A variable name must begin with a letter or an underscore (‘_’), which may be followed by a sequence of letters, digits(0-9), or underscores.  The first character in a variable name cannot be a digit.
·         A variable name should not contain any embedded spaces or symbols, such as                  “ ?!@#$%^&*()[]{}.,’;:\/”.  However, an underscore can be used wherever a space is required, like High_Score.
·         A variable name must be unique.  For example, to store four different numbers, four unique variable names need to be used.
·         A variable name can have any number of characters.
·         Keywords cannot be used as variable names.  For example, you cannot declare a variable named class as it is a keyword in C#.
The examples of valid variable names are:
·         Game_Level
·         High_Score
·         This_variable_is_very_long
The examples of invalid variable names are:
·         #score
·         2strank

C# is a case-sensitive language.  This means that the TennisPlayerName variable is not the same as the tennisplayername variable.  In other words, Uppercase letters are considered distinct from lowercase letters.

You can declare and initialize variables by using the following syntax:
<data_type> <variable_name> = <value>;
In the preceding syntax, the   <data_type> represents the kind of data type that will be stored in a variable and <value> specifies the value that needs to be stored in the variable.
Consider the following statement that declares a variable:
Int age=1;
The preceding statement declares a variable named age of the int data type.  In addition, the statement initializes the variable with the value, 1.  The int data type is used to store numeric data (integers).
Consider the following statement:
Char choice=’y’
The preceding statement declares the variable choice of the char data type and initializes the variable with the value, y.
C# provides various built – in data types.  Built-in data types are predefined data types that can be directly used in a program to declare variables.

The following table lists some of the built in data types in C#

Predefined Type
Range of values

0 to 65535
-2147483648 to 2147483647
-3.402823 * 10^38 to -1.401298*10^-45(for negative values)
1.401298*10^-45 and 3.402823*10^38(for positive values)

-1.79769313486232E308 to -4.940656455841247E-324(for negative values) and 4.94065645841247E-324 to       1. 79769313486232E308(for positive values)
True or False
Variable length
0-2 billion Unicode characters

The #Bytes column in the preceding table specifies the bytes that are required to store the value in the memory.
C# supports the following data types:
·         Value types: The value types directly contain data.  Some examples of the value types are char, int, and float, which can be used for storing alphabets, integers, and floating point values, respectively.  When you declare an int variable, the system allocates memory to store the value.
·         References types: The reference type variables, instead of containing data, contain a reference (address) to the data stored in the memory.  More than one reference type variable can be created to refer to the  same memory location.  This means that if the value in the referenced memory location is modified, all the referring  variables automatically reflect the changed value.  The example of reference type is string data type.

           To understand how to store value in a variable, consider the following code snippet:
                 int Number;
                 Number = Convert.ToInt32(Console.ReadLine());
          In the preceding code snippet, the  Console.ReadLine() method is used to accept data from the user and store it in the variable named Number.  The Console.ReadLine() method is a method of the Console class, which is a part of the System namespace.

          Further, the Convert.ToInt32() method converts the data provided by the user to the int data type.  You need to do this conversion because the Console.ReadLine() method, by default, accepts the data in the string format.  The Convert() method explicitly informs the compiler to convert one data type to the other.  This is known as explicit conversion.
         In addition to explicit conversion, there are instances when the compiler performs implicit conversions, which are done automatically(without any explicit coding).  For example, implicit conversion converts the int data type to float or float data type to int, automatically.

No comments:

Post a Comment