Visual Programming Lab Manual
Visual Programming Lab Manual
Visual Programming Lab Manual
LAB#2
Objectives:
Demonstrate the usage of Data types, Operators and Expression, Methods, Class
Theory:
In this lab students will be introduced with variable, data types, operator, class, method
and expression.
C# Variable
A variable is a name of memory location. It is used to store data. Its value can be
changed and it can be reused many times. It is a way to represent memory location
through symbol so that it can be easily identified. The basic variable type available in C#
can be categorized as:
A variable name can start with alphabet and underscore only. It can't start with
digit.
No white space is allowed within variable name.
A variable name must not be any reserved word or keyword e.g. char, float etc.
C# Data Types
VISUAL PROGRAMMING LAB MANUAL
A data type specifies the type of data that a variable can store such as integer, floating,
character etc. There are 3 types of data types in C# language.
Value Data Type: 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VariableDeclarationExample
{
class Program
VISUAL PROGRAMMING LAB MANUAL
{
static void Main(string[] args)
{
//variable declaration
short a;
int b;
double c;
//variable initializatioin
a = 10;
b = 20;
c = 2015.65;
Console.WriteLine("The vale of a = {0}, b = {1}, c = {2}",a,b,c);
Console.ReadLine();
}
}
}
To get the exact size of a type or a variable on a particular platform, you can use
the sizeof method. The expression sizeof(type) yields the storage size of the object or
type in bytes. Following is an example to get the size of int type on any machine –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataTypeApplication
{
class Program
VISUAL PROGRAMMING LAB MANUAL
{
static void Main(string[] args)
{
Console.WriteLine("Size of int : {0} bytes",sizeof(int));
Console.WriteLine("Size of char: {0} bytes", sizeof(char));
Console.WriteLine("Size of double : {0} bytes", sizeof(double));
Console.WriteLine("Size of bool : {0} bytes", sizeof(bool));
Console.ReadLine();
}
}
}
Output:
Size of int : 4 bytes
Size of double : 8 bytes
Size of char : 2 bytes
Size of bool : 1 bytes
Reference Type
The reference types do not contain the actual data stored in a variable, but they contain
a reference to the variables.
In other words, they refer to a memory location. Using multiple variables, the reference
types can refer to a memory location. If the data in the memory location is changed by
one of the variables, the other variable automatically reflects this change in value.
Example of built-in reference types are: object, dynamic, and string.
Object Type
The Object Type is the ultimate base class for all data types in C# Common Type
System (CTS). Object is an alias for System.Object class. The object types can be
assigned values of any other types, value types, reference types, predefined or user-
defined types. However, before assigning values, it needs type conversion.
Note: When a value type is converted to object type, it is called boxing and on the other
hand, when an object type is converted to a value type, it is called unboxing.
Example:
object obj;
VISUAL PROGRAMMING LAB MANUAL
Dynamic Type
You can store any type of value in the dynamic data type variable. Type checking for
these types of variables takes place at run-time.
Syntax for declaring a dynamic type is −
dynamic d = 20;
Dynamic types are similar to object types except that type checking for object type
variables takes place at compile time, whereas that for the dynamic type variables takes
place at run time.
Note: The constants refer to fixed values that the program may not alter during its
execution. These fixed values are also called literals.
Defining Constants
Constants are defined using the const keyword. Syntax for defining a constant is −
The following program demonstrates defining and using a constant in your program −
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstantExample
{
class Program
{
VISUAL PROGRAMMING LAB MANUAL
double areaCircle = pi * r * r;
Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
Console.ReadLine();
}
}
}
String Type:
The String Type allows you to assign any string values to a variable. The string type is
an alias for the System.String class. It is derived from object type. The value for a string
type can be assigned using string literals in two forms: quoted and @quoted.
For example,
@"Hello";
Pointer Type
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 for declaring a pointer type is −
type* identifier;
For example,
VISUAL PROGRAMMING LAB MANUAL
char* cptr;
int* iptr;
Type Casting:
Type conversion is converting one type of data to another type. It is also known as Type
Casting. In C#, type casting has two forms:
Implicit type conversion: These conversions are performed by C# in a type-safe manner.
For example, are conversions from smaller to larger integral types and conversions
from derived classes to base classes.
Explicit type conversion: These conversions are done explicitly by users using the pre-
defined functions. Explicit conversions require a cast operator.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TypeCastingExample
{
class Program
{
static void Main(string[] args)
{
double d = 542.56;
int i;
i = (int)d;
Console.WriteLine("The vale of i is = {0}", i);
Console.ReadLine();
}
}
}
logical manipulations. C# has rich set of built-in operators and provides the following
type of operators
Assignment Operators [=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=]
VISUAL PROGRAMMING LAB MANUAL
1. Write a program that contains two variables of type int and perform all arithmetic
operations.
2. Write a program that performs all the logical operations on two variables.
VISUAL PROGRAMMING LAB MANUAL
Name:
Roll #:
Date:
(Subject Teacher)
Remarks: