Chapter 4
Chapter 4
1. Variables
Variables are containers for storing data values. The variables in C#, are categorized into the following
types:
• Value types: Value type variables can be assigned a value directly. They are derived from the
class System.ValueType. The value types directly contain data. Some examples are int, char,
and float, which stores numbers, alphabets, and floating point numbers, respectively. When you
declare an int type, the system allocates memory to store the value.
• Reference types: The reference types do not contain the actual data stored in a variable, but
they contain a reference to the variables.
• Pointer types: Pointer type variables store the memory address of another type. Pointers in C#
have the same capabilities as the pointers in C or C++.
Syntax:
To get the exact size of a type or a variable on a particular platform, you can use the sizeof() method.
data_type.MaxValue() method gives the numerical value the variable will take the most.
data_type.MinValue() method gives the numerical value the variable will take the least.
The precision of a floating point value indicates how many digits the value can have after the decimal
point. The precision of float is only six or seven decimal digits, while double variables have a precision
of about 15 digits. Therefore it is safer to use double for most calculations.
✓ Boolean value
✓ Textual expressions
char x = 'x';
Ex : Types
• Defining Constants
Constants are defined using the const keyword.
Syntax:
namespace ConsoleApp1
{
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);
Console.ReadLine();
}
}
}
• Local/Global Variables:
A scope in any programming is a region of the program where a defined variable can have its existence
and beyond that variable it cannot be accessed. Let us understand what are local and global variables.
➢ Inside a function or a block which is called local variables. A local variable is used where the
scope of the variable is within the method in which it is declared. They can be used only by
statements that are inside that function or block of code.
using System;
public class Program
{
public static void Main()
{
int a;
a = 100;
// local variable
Console.WriteLine("Value:" + a);
}
}
➢ Outside of all functions which is called global variables. A global variable is a variable
accessible anywhere, for example a field "counter" type integer. The global variable can be
accessed from any function or class within the namespace.
C# is an object-oriented programming (OOP) language and does not support global variables
directly. You need to add the static keyword before class and type.
Homework 1:
using System;
namespace ConsoleApp1
{
class Bread
{
public int counter;
}
class Program
{
static void Main(string[] args)
{
// instance of class "Butter" required
Bread a = new Bread();
a.counter = 5;
Console.WriteLine("Bread: " + a.counter);
// no instance required!
Butter.counter = 7;
Console.WriteLine("Butter: " + Butter.counter);
}
}
}