Variables in .net(C#)

Friday, 3 June 2016

Variables in .net(C#)

Variables is a storage area of the computer's memory which is mainly used to manupulate these store

As there are different types of values used in an application, so different storage area required to store these vauues.
To store a value in a memory, you must provide only two pieces of information: the name of the variable and the Data type (
information that would be stored in the memory reserved for that variable).

Each variable in the variable list has the following syntax and parts:
variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]
Where,
• variablename: is the name of the variable
• boundslist: optional. It provides list of bounds of each dimension of an array variable.
• New: optional. It creates a new instance of the class when the Dim statement runs.
• datatype: Required if Option Strict is On. It specifies the data type of the variable.
• initializer: Optional if New is not specified. Expression that is evaluated and assigned to the variable when it is created.

Names in C#

To name the variables of your program, you must follow strict rules. In fact, everything else in your application must have a name.
C# uses a series of words, called keywords, for its internal use. This means that you must avoid naming your objects using one of these keywords. They are:
abstract,const,extern,int,out,short,typeof,as,continue,false,interface,override,sizeof,uint etc

Different types of variables are the in .net 

Constants Variables

 A constant is an access modifier that enables you to declare a variable whose value doesn't change throughout the lifetime of your application
Before using a constant. A constant that is part of the C# language can be accessed anywhere in your code. By default constant are static,
hence you cannot define a constant type as static.

Custom Constants
Once a constant has been created and it has been appropriately initialized, you can use its name where the desired constant would be used.
To initialize a constant variable, the value on the right side of the assignment operator "=" must be a constant or a value that the compiler can
determine as constant. Instead of using a known constant, you can also assign it another variable that has already been declared as constant.
example: public const int X = 10;

Built-in Constants
 The C# language also provides various constants. Some constants are part of the C# language. Some other constants are part of the .NET Framework.
Those constant are normally defined in the System namespace.
PI: PI is a constant used as the ratio of the circumference of a circle to its diameter. PI is defined in Math. To use it, you would type Math.PI.
examples :const decimal myPI = 3.4799897;

Using Static(C#) and Shared Variables(VB)

Static and shared variables are special variable types that retain their values. In C#, the keyword Static is equivalent in function to the Visual Basic
.NET keyword Shared. The static keyword is used to specify a static member, which means static members are common to all the objects and they do not tied
to a specific object. This keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with
 indexers, destructors, or types other than classes.
uses of static key ward
If the static keyword is applied to a class, all the members of the class must be static.
Static methods can only access static members of same class. Static properties are used to get or set the value of static fields of a class.
Static constructor can't be parameterized. Access modifiers can not be applied on Static constructor, it is always a public default constructor
 which is used to initialize static fields of the class.
class MyClass
{
 static int X = 10;
 int Y = 20;
 public static void display()
 {
 Console.WriteLine(X);
 Console.WriteLine(Y); //error, since you can access only static members
 }
}

ReadOnly variables

If we use readonly key wards on the left side of a veriables then we can change the value of this variable during run time but only
through non static constructor.
class MyClass
{
 readonly int X = 10; // initialized at the time of declaration
 readonly int X1;

 public MyClass(int x1)
 {
 X1 = x1; // initialized at run time
 }
}
Explicitly, you can specify a readonly field as static since, like constant by default it is not static. Readonly keyword can be apply to value type and
reference type both.


Using the New Keyword

The New keyword is used both c# and vb for creating object instances. When we declare a variable as an integer, you can say that you've declared a variable of
type integer. Each time you create a new class file in your projects, you're creating a new type. By adding properties, methods, structures, and variables
to the class, you're creating the members of the class. Its available to the variable X:

Class1 x = new Class1;
After you've declared X as a new instance of the class Class1, you have access to the members of Class1 using the dot notation. For example, if Class1 has a
public string variable named firstName, you can access it as follows:

Each time you use the New keyword, you're creating an instance of a class. The variable holds the instance, which then has access to all the members in the
class instance you've declared.