Variables and constants

 Variables and constants


Content

 


1. What are the constants and variables in the program?

Constants and variables are the main objects, that are used in program. Constants and variables – this is a named objects for which the memory is allocated.

When the constant is declared, then value of constant must be set necessarily in the program. Unlike the variable the value of constant cannot be changed by a program way. If constant is already declared, then any trying to assign a new value for constant will cause the message of compiler about error.

All variables and constants must be declared before their using. It is necessary to tell the compiler about data type, which is saved in a variable. If known the variable type, then the compiler carries out the corresponding compilation of any operator, where given variable is used.


2. How to declare the constant in C#? Examples of constants declaration.

A constant is declared by using the following operator:

const type constant_name = value;

where

constkey word, that this is a constant; type – the type of data, that are saved in the constant; constant_name – the name of constant; value – the value of constant that is unchanged in program.

Examples of constant’s definitions.

const decimal m = 333897987.3949M; // the constant of type "decimal"
const double pi = 3.1415; // the constant of type "double"
const bool YES = true; // the constant of type "bool"
const int Max = 255; // the constant of type "int"
const char AnswerYes = 'Y'; // the constant of type "char"
const string Str = "Constant String"; // the constant of type "string"


3. How declare the variable in program? Examples of definitions of variables.

The variable is declared by using the following statement:

type variable_name;

where type – the type of data which are saved in the variable; variable_name – the name of variable.

You can declare the variable of any type that is in program. The variable’s type can be also a value type.

An example of declaration the variable that has a value type.

double d, f; // 2 variables of type "double"
int i, j; // 2 variables of type "int"
bool answer; // variable of type "bool"
char c; // variable of type "char"
string s; // varialbe of type "string"

d = 2.939;
f = -29309.39;
i = 50;
j = -300;
c = '=';
s = "This is a text";
answer = false;


4. Example of declaration the variable that is structure.

In the C# language the structure belongs to the value type but not to the class type.

Example of declaration the variable that is the structure of type MyPoint. The MyPoint structure defines the coordinates on the plane.

...

struct MyPoint
{
    public int x;
    public int y;
};

...

MyPoint mpt; // definition of variable "mpt" of type "MyPoint"

// Assigning the values to the internal fields of variable "mpt"

mpt.x = 20;
mpt.y = -30;

...


5. Example of declaration the variable that is enumeration.

In the example below the variable of type “Months” is declared. This variable is enumeration.

enum Months { January, February, Murch, April, May, Jun,
              July, August, September, October, November, December };

...

Months mn;
Months mn2;

mn = Months.January;
mn2 = mn + 1; // mn2 = Months.February

...


6. How to initialize the variable? Examples of initialization the variables.

Initialization allows to set the initial value for variable when it is declared.

The general form of variable’s initialization is following:

type variable_name = value;

where

type – the type of data, which are saved in the variable; variable_name – the name of variable; value – the specific value, that is given when variable is created.

Examples of initialization the variables:

int i = -463; // set the initial value -463 for variable "i"
double d = 2093.9903; // set the value 2093.9903 for variable "d"
float f = -10039.39f;
char c = '%';
short sh = -30;
bool b = true;

You can initialize the variables selectively. For example:

long l1 = 20003909, l2, l3 = 48938938984; // the variables "l1" and "l3" are initialized
string s, s1 = "Hello ", s2 = "world!", s3 = s1 + s2; // s3 = "Hello world!"


7. How in C# is realized the dynamical initialization of variables?

When you initialize the variable to dynamical way are used the expressions but not constants or literals. These expressions at the time of declaring the variable must be valid.

const double pi = 3.1415926535;
double r = 2; // radius
double s = pi * r * r; // s = 12.566370614
double h = 3, v = s * h; // v = 37.699111842 - the volume of a cylinder of radius r = 2 and height h = 3


8. How in C# are declared implicitly typed variables?

From the version C# 3.0 compiler can define the type of local variable. This type is defined on the base of value, which is initializes the variable. The some literal corresponds to this value according to C# syntax. This variable is called as implicitly typed variable.

To declare the implicitly typed variable is used the key word “var“. In this case the initialization of variable is necessarily.

The general view of implicitly typed variable is the following:

var variable_name = value;

where

var – key word; variable_name – the name of variable; value – the specific value, that is set when variable is created.

Examples of definition implicitly typed variables:

var pi = 3.1415; // pi - of type "double"
var r = 3.39948f; // r - of type "float"
var h = 4393489348934.03290889m; // h - of type "decimal"
var i = 23; // i - of type "int"
var j = 38L; // j - of type "long"
var k = 30920039092UL; // k - of type "ulong"
var c = '&'; // c - of type "char"
var s = "C# 4.0"; // s - of type string

// var z = 3.8, y = 9.5; - error: at the same time can be described only one implicitly typed variable


9. How much implicitly typed variables you can declare at the same time?

Simultaneously, you can declare only one typed variable.

In the following description, there is an error, since the two variables are described:

// error!
var z = 3.8, y = 9.5;


10. Example of declaration of implicitly typed variable that is enumeration.

 Example of declaration the implicitly typed variable that is enumeration of type “DaysOfWeed“.

...

enum DaysOfWeek
{
    Sunday, Monday, Tuesday, Wednesday,
    Thursday, Friday, Saturday
};

...

var dw = DaysOfWeek.Saturday; // "dw" of type DaysOfWeek


11. What determines the scope of a local variable?

The scope of a local variable is defined by code block. The code block starts with an open braces ‘{‘ and ends closed brace ‘}‘.

...

{
    // the scope of a local variable

    ...

}

...

The scope determines visibility of constants and variables without

The scope defines the visibility of the names of constants and variables, without further clarification. Also, the scope defines the lifetime of the local variable. In C# the scope is determined by a class or by a method.

The scopes can be nested. If the code block is created then the nested scope is also created. In this case, the outer scope encompasses the inner scope. It means that local variables and constants, which are declared in the outer scope, will be accessed in the inner scope:

...

{
    // outer block
    int i;
    double d;

    ...

    {
        // inner block
        int z;

        ...

        i = 25;   // here is the access to the variables i and d
        d = 2329.344;
        z = -209; // z is only available in the inner block

        ...

    }

    // Error! Variable 'z' is unavailable here
    // z = 123;

}

...