Module 5 - More About Variables

  • Uploaded by: api-19796528
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Module 5 - More About Variables as PDF for free.

More details

  • Words: 1,056
  • Pages: 27
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 Conversion of Value type to Reference type. 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 

Implicit Conversion 



& Explicit Conversions

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; System.Console.WriteLine("\n Source:

"+source.ToString()); System.Console.WriteLine("\n Dest:"+dest.ToString()); System.Console.ReadLine();

Example - Explicit Conversions 

string sSource="10"; int iDest; iDest=Convert.ToInt32(sSource); Console.WriteLine(iDest);



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 } Now the value of Red is 0, Blue is 1, Green is 2 

enum Color{ Red=2, Blue,Green,Yellow }; Now Red is 2, Blue is 3, Green is 4, Yellow is 5

Enumerations.. 

The value of an enum member can be assigned using a constant expression initializer.



In case an enum member has no initializer, its value is set as follows: 

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 :enum Color { Red= Pink, Green, Blue, Pink }

Example 1 - Enumerations using System; namespace sampleEnum { class sampleEnum { enum Days { Sun = 1, Mon, Tue = 7, Wed, Thu, Fri, Sat };

}

}

static void Main(string[] args) { int x = (int)Days.Sun; int y = (int)Days.Fri; int z = Convert.ToInt16(Days.Wed); Console.WriteLine("Sun = {0}", x); Console.WriteLine("Fri = {0}\n", y); Console.WriteLine("wed = {0}\n", z); }

Structs 

A structure is a derived data type that allows a programmer to group data elements belonging to the different data types.



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.



All elements in an array occupy continuous memory locations.



Each element in the array is referred to by the array name and the array subscript number also known as the index number.



The subscript number of an array starts from 0.



Arrays in C-Sharp can be single– or multi–dimensional, each row have same size.



The most commonly used array type is single–dimensional.



Rows of different size is called Jagged Arrays i.e., Array of Arrays

Arrays Creation 

Array Declaration int [ ] MyIntArray;



Memory Allocation MyIntArray = new int[5];



Initialization MyIntArray[0]=1 // Assigns a value 1 to the first element MyIntArray[1]=2 // Assigns a value 2 to the second element MyIntArray[2]=3 // Assigns a value 3 to the third element MyIntArray[3]=4 // Assigns a value 4 to the fourth element MyIntArray[4]=5 // Assigns a value 5 to the fifth element



Example :int[ ] MyIntArray = new int[5]; ={1,2,3,4,5};

int[ ] MyIntArray

int[ ] MyIntArray =new int[5]{1,2,3,4,5};

Example - Arrays 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 Console.WriteLine(“Displaying the contents of array”); for (int j=0; j<3; j++) { Console.WriteLine(arr[j]); } } } Output of the code is: 0 1 2

Jagged Arrays 

In Single, Multi-dimensional arrays each row has the same size.



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; jagged = new int [2][ ]; jagged [0] = new int [3]; jagged [1] = new int [4]; int [ ] [ ] jagged = {new int [ ] {1,2,3}, new int [ ] {1,2} };

Example – Jagged Arrays Using System; namespace Arrays { class jaggedarrays { public static void Main() { int[ ] [ ] jagged = {new int [] {1,2,3}, new int [] {1,2}} ;

Example – Jagged Arrays.. foreach(int[ ] a in jagged) { foreach(int x in a) { Console.WriteLine(x); } } }

More About Variables - Flashback 

Type Conversion 

Boxing



UnBoxing



Implicit & Explicit Type Conversion



Enumerations



Structs



Arrays 

One Dimensional



Two Dimensional



Jagged

Session Ends

Exercise

Relax

Related Documents