06 C# Variables

I apologize for that absence but let’s continue what we had started. 🙂

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.

and here’s a link declares the basic value types provided in C#:

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;

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);


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.

C# Constants and Literals 

The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.
The constants are treated just like regular variables except that their values cannot be modified after their definition.

Character Constants

Character literals are enclosed in single quotes, e.g., ‘x’ and can be stored in a simple variable of char type. A character literal can be a plain character (e.g., ‘x’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u02C0’).
There are certain characters in C# when they are preceded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Here, you have a list of some of such escape sequence codes:

The following program demonstrates defining and using a constant in your program:

using System;
 namespace DeclaringConstants
 class Program
 static void Main(string[] args)
 const double pi = 3.14159;
 // constant declaration double r;
 Console.WriteLine("Enter Radius: ");
 r = Convert.ToDouble(Console.ReadLine());
 double areaCircle = pi * r * r;
 Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);

And now we have already know the Variables provided in C# and the Constants 
in the next time we will know the Operators in C#
So, Stay tuned for more 🙂
and feel free to contact me  any time. 


4 thoughts on “06 C# Variables

  1. Good examples for C# variables. Thanks.


  2. Keep it coming, wrsrite, this is good stuff.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s