Atsede
Atsede
4) Application domains
It is used to isolate the process of different applications and can be defined by
.NET framework.
C# - Variables
Advertisements
Previous Page
Next Page
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.
The basic value types provided in C# can be categorized as −
Type Example
Integral types sbyte, byte, short, ushort, int, uint, long, ulong, and char
C# also allows defining other value types of variable such as enum and reference
types of variables such as class, which we will cover in subsequent chapters.
Defining Variables
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, 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;
You can initialize a variable at the time of definition as −
int i = 100;
Initializing Variables
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 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'. */
To define constant values of integral types (int, byte, and so on) use an enumerated
type. For more information, see enum.
To define non-integral constants, one approach is to group them in a single static class
named Constants. This will require that all references to the constants be prefaced with
the class name, as shown in the following example.
Example
C#Copy
static class Constants
{
public const double Pi = 3.14159;
public const int SpeedOfLight = 300000; // km per sec.
}
class Program
{
static void Main()
{
double radius = 5.3;
double area = Constants.Pi * (radius * radius);
int secsFromSun = 149476000 / Constants.SpeedOfLight; // in km
}
}
The use of the class name qualifier helps ensure that you and others who use the
constant understand that it is constant and cannot be modified.
using System;
namespace VariableDefinition {
class Program {
static void Main(string[] args) {
short a;
int b ;
double c;
/* actual initialization */
a = 10;
b = 20;
. An array stores a fixed-size sequential collection of elements of the same type. An array is
used to store a collection of data, but it is often more useful to think of an array as a collection
of variables of the same type stored at contiguous memory locations. c = a + b;
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result −
a = 10, b = 20, c = 30
. An array stores a fixed-size sequential collection of elements of the same type. An array is
used to store a collection of data, but it is often more useful to think of an array as a collection
of What is conditional control?
BASIC
A loop decides how many times to execute another statement. There are three
kinds of loops: