C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Variables, Constants and ReadOnly


Posted Date: 15 Aug 2008    Resource Type: Articles    Category: General

Posted By: Gaurav Arora       Member Level: Gold
Rating:     Points: 10



This article's scope to define the relevant words as per title and limits to practical slution.

One can declare Variables in C# as:
datatype identifier;

Rules:

  • Variables should be initialized before use

  • Declared Variables supersedes earlier declared variable within scope

  • Initialized value must have relevant to their type

  • Must be start with alphabets

  • You can use alphanumeric

  • No special or reserved keywords use as variable declaration.



e.g. int I, i; //Valid
I=s; //Invalid
int ii = 1; bool II = true;
int h=5, bool HH = true; //wrong



// File name : variables.cs

using System;

namespace CSharp.AStepAhead.variables
{
public class variables
{
static int i;
public static void pubVar()
{
int j = 5; //Its scope is local to pubVar()
i = i+j;
}
public static void Main()
{
int j = 1;
Console.ReadLine();
Console.WriteLine("Value of i={0}, and j={1}: Before calling pubVar()",i,j);
Console.ReadLine();
i = 100;
pubVar();
Console.WriteLine("Value of i={0}, and j={1}: After calling pubVar()", i, j);
Console.ReadLine();
return;
}
}
}


Scope of Variables
The scope of a variable is nothing but it's a surrounding of code where the variable can be accessed. In other words the scope of a variable is within the curly braces {}.

In above, the value of 'j' remains unchanged throughout the program even after supplying different values to it. Its because the scope of variable 'j' is local upto its surrounding of code.


Constant as it implies by name it's a variable whose value cannot be changed throughout its lifetime.
e.g . const int i = 208;

There are some points keep in mind while declaring constant variables:

  1. Constant variables must be initialized while define.

  2. The const value should be a computable value at compile time.


  3. You can supply constant variable to another variable.



ReadOnly as it implies by name it's a variable which are specifically declare for readonly purpose value may be changed throughout its lifetime.
e.g . readonly int i;


Difference between Constant and ReadOnly
Constant variables must be initialized at the declaration time, there is no need to initialization at the declaration stage in case of readonly variables.
Constant variables are static by nature so no need to create an instance to access these.

The value of Constant remains unchanged through-out their lifetime, but you can change the value to read-only variables.

Note: You can change the value of readonly variables but not in case of constant variables.

The following example tells everything by itself:


// File name : constantreadonlyvariables.cs
using System;
namespace CSharp.AStepAhead.ConstReadOnlyVariables
{
public class ConstReadOnlyVariables
{
class ReadOnlyClass
{
public int x;
public readonly int y = 25;
public readonly int z;

public ReadOnlyClass()
{
z = 24;
}

public ReadOnlyClass(int x1, int y1, int z1)
{
x = x1;
y = y1;
z = z1;
}
}
class ConstClass
{
public const int xx = 208;
public int i;
public readonly int jj;
public ConstClass()
{
i = xx + 9;
}
public ConstClass(int i1, int jj1)
{
i = i1;
jj = jj1;
}
public ConstClass(int i1)
{
//xx = i1; //invalid uncomment this to check
i = i1;
}
}

static void Main()
{
//ReadOnly Test
Console.WriteLine("\nReadOnly Class Output\n");
ReadOnlyClass myCls1 = new ReadOnlyClass();
Console.WriteLine("myCls1: x={0}, y={1}, z={2}", myCls1.x, myCls1.y, myCls1.z);
ReadOnlyClass myCls2 = new ReadOnlyClass(9, 18, 27);
Console.WriteLine("myCls2: x={0}, y={1}, z={2}", myCls2.x, myCls2.y, myCls2.z);

Console.WriteLine("\nConstant Class Output\n");
ConstClass myCls3 = new ConstClass();
int j = ConstClass.xx; //no need to create instance, const variables static by nature
Console.WriteLine("myCls3: i={0},jj={1} xx={2}", myCls3.i, myCls3.jj, j);
ConstClass myCls4 = new ConstClass(226,9);
j = ConstClass.xx+1;
Console.WriteLine("myCls4: i={0},jj={1},xx={2}", myCls4.i,myCls4.jj,j);


ConstClass myCls5 = new ConstClass(9);
j = ConstClass.xx;
Console.WriteLine("myCls5: i={0}, jj={1},xx={2}", myCls5.i,myCls5.jj,j);

}
}
}


In above example, we have created two classes one is for readonly variables and another is for constant variables. As we have checked the difference between both, above. So, we have tried to show the same here, when we tried to change the value of a constant field then it will generate error, uncomment the statement in above example.

For more details, visit http://www.msdotnetheaven.com/ChaptersCsharp/csharplanguagei.htm




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Quesion  .  Interview  .  C# syntax  .  .net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Introduction to Share point 2007
Previous Resource: C# Data Types
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

email fax service

Contact Us    Privacy Policy    Terms Of Use