Module 5 - More About Variables
Module 5 - More About Variables
By
SRIRAM . B
Overview
Type Conversion
Boxing
UnBoxing
Implicit & Explicit Type Conversion
Enumerations
Structs
Arrays
One Dimensional
Two Dimensional
Jagged
Type Conversions
Boxing
int i = 10;
string s;
s = i.ToString();
Console.WriteLine(s);
Console.ReadLine();
Type Conversions
UnBoxing
Conversion of Reference type to Value type.
int i;
string s = "10";
i = Int32.Parse(s);
Console.WriteLine(s);
Console.ReadLine();
Implicit & Explicit Conversions
Implicit Conversion
It requires no work on our part, and no additional code.
i.e. Converting from higher end to lower end datatype.
Explicit Conversion
It occurs when we explicitly ask the compiler to convert a value from one data
type to another. It requires the extra code and format of the code may depend
on the exact conversion method.
i.e. Converting from lower end to higher end datatype requires explicit
conversion.
Example - Implicit Conversions
• int i=10;
double j;
j=i;
System.Console.WriteLine(j);
• ushort dest;
char source='A';
dest=source;
byte bDest;
short shSource=23;
bDest=(byte)shSource;
Console.WriteLine("Source:" + shSource.ToString());
Console.WriteLine("Dest:"+bDest.ToString());/
Example - Explicit Conversions
shSource=523;
bDest=unchecked((byte)shSource);
Console.WriteLine("Source:" + shSource.ToString());
Console.WriteLine("Dest:"+bDest.ToString());
Console.ReadLine();
Enumerations
An enum type declaration defines a type name for a related set of constants.
Enums are used when you need to make a choice from a fixed number of choices
at compile-time.
Syntax :
enum typename
value 1,
value 2,
value 3,
.................
}
Enumerations..
For example,
enum Color //Declares an enum of type int
{
Red, //Members
Green,
Blue
}
The first enum member declared using the enum type has the value zero.
The values of the successive enum members will have the increased value of
the previous enum member by one.
The increased values should be within the range of values that can be
represented by the underlying enum type.
Enumerations..
Multiple enum members can share the same associated value :-
enum Color { Red, Green, Blue, Pink=Blue }
This shows an enum that has two enum members, Blue and Pink, which have the
same associated value.
Even though two members can have the same values associated with them it
returns an error :-
A struct type is a value type that can contain constructors, constants, fields,
methods, properties, indexers, operators and nested types.
Syntax :-
<modifier> struct <structurename>
{
<members> \\ Elements in the structure called Members
<members>
}
Modifier may be public, private, protected & internal
Example 1 - Stuct
using System;
namespace Structure
{
class sampleStructure
{
static void Main(string[] args)
{
xxx x = new xxx(10);
Console.WriteLine(x.i);
x.abc();
Console.ReadLine();
}
struct xxx
{
public int i;
public xxx(int j)
{
i = j;
}
public void abc()
{
System.Console.WriteLine("abc");
}
}
}
}
Arrays
An array is a data structure that contains a collection of elements of the
same type, which are referenced by a common name.
Each element in the array is referred to by the array name and the array
subscript number also known as the index number.
Arrays in C-Sharp can be single– or multi–dimensional, each row have same size.
using System;
class Array_type
{
public static void Main()
{
int[ ]arr;
arr=new int[3];
Console.WriteLine(“Initialising the array”);
for (int i=0; i<3; i++)
{
arr[i]=i;
}
Example - Arrays
Rows with different size are called Jagged Arrays, To do this we need to have an
array where each element is another array. i.e., Array of Arrays
Syntax :-
int [ ] [ ] jagged;